Categories
JavaScript Answers

How to send a message to a particular client with Node.js socket.io?

Sometimes,. we want to send a message to a particular client with Node.js socket.io.

In this article, we’ll look at how to send a message to a particular client with Node.js socket.io.

How to send a message to a particular client with Node.js socket.io?

To send a message to a particular client with Node.js socket.io, we can call sockets.in before calling emit.

For instance, we write

const socket = io.connect('http://localhost');
socket.emit('join', {
  email: 'user@example.com'
});

on client side to connect to the socket.io server.

Then on server side, we write

const io = require('socket.io').listen(80);

io.sockets.on('connection', (socket) => {
  socket.on('join', (data) => {
    socket.join(data.email);
    io.sockets.in('user@example.com').emit('msg', {
      msg: 'hello'
    })
  });
});

to create a socket.io server.

And we listen for 'join' events that are emitted by clients.

We get the data from the object in the 2nd argument of emit from data.

Then we call io.sockets.in with 'user@example.com' and then call emit to send data to the client with email 'user@example.com'

Conclusion

To send a message to a particular client with Node.js socket.io, we can call sockets.in before calling emit.

Categories
JavaScript Answers

How to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js?

Sometimes, we want to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js.

In this article, we’ll look at how to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js.

How to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js?

To fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js, we should make sure the passport.initialize method is called before other Passport middlewares are added.

For instance, we write

const app = express();

app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({
  extended: true
}));
app.use(require('express-session')({
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());

to call passport.initialize and call app.use with the returned middleware before we call passport.session.

This way, Passport is initialized before we do anything else with Passport, and we avoid the error.

Conclusion

To fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js, we should make sure the passport.initialize method is called before other Passport middlewares are added.

Categories
JavaScript Answers

How to use the Node.js Morgan logger?

Sometimes, we want to use the Node.js Morgan logger.

In this article, we’ll look at how to use the Node.js Morgan logger.

How to use the Node.js Morgan logger?

To use the Node.js Morgan logger, we can use it with log4js.

For instance, we write

const express = require("express");
const log4js = require("log4js");
const morgan = require("morgan");

//...

const theAppLog = log4js.getLogger();
const theHTTPLog = morgan({
  "format": "default",
  "stream": {
    write: (str) => {
      theAppLog.debug(str);
    }
  }
});

//...

const theServer = express();
theServer.use(theHTTPLog);

to create a logger instance with log4js.getLogger.

Then we call morgan with an object sets some options.

We set stream.write to a function that calls theAppLog.debug to use log4js with morgan to do the logging.

And then we call use with theHTTPLog to use the logger created by morgan to do the logging.

Conclusion

To use the Node.js Morgan logger, we can use it with log4js.

Categories
JavaScript Answers

How to remove debugging from socket.io?

Sometimes, we want to remove debugging from socket.io.

In this article, we’ll look at how to remove debugging from socket.io.

How to remove debugging from socket.io?

To remove debugging from socket.io, we can set the log option to false when we call listen.

For instance, we write

const io = require('socket.io').listen(app, {
  log: false
});

to call listen with log set to false to disable logging with socket.io.

We can also set the log level with

const io = require('socket.io').listen(app);

io.set('log level', 1);

to set log level to 1 to reduce the level of logging.

Conclusion

To remove debugging from socket.io, we can set the log option to false when we call listen.

Categories
JavaScript Answers

How to set up two different static directories with Node.js Express framework?

Sometimes, we want to set up two different static directories with Node.js Express framework.

In this article, we’ll look at how to set up two different static directories with Node.js Express framework.

How to set up two different static directories with Node.js Express framework?

To set up two different static directories with Node.js Express framework, we can call express.static and app.use with different static file paths.

For instance, we write

app.use("/public", express.static(__dirname + "/public"));
app.use("/public2", express.static(__dirname + "/public2"));

to expose the /public URL path to __dirname + "/public" with express.static.

Likewise, we expose the /public2 URL path to __dirname + "/public2" with express.static.

Conclusion

To set up two different static directories with Node.js Express framework, we can call express.static and app.use with different static file paths.