Categories
JavaScript Answers

How to add Node.js client for a socket.io server?

To add Node.js client for a socket.io server, we use the socket.io-client package.

For instance, on the Node.js client, we add

const io = require("socket.io-client");
const socket = io.connect("http://localhost:3000", { reconnect: true });

socket.on("connect", (socket) => {
  console.log("Connected!");
});
socket.emit("CH01", "me", "test msg");

to connect to localhost:3000 with io.connect.

We call on to listen for connection events.

And we call emit to emit the 'CH01' event with 'me' and 'test msg' as the content.

On server side, we write

const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);

io.on("connection", (socket) => {
  console.log("connection");

  socket.on("CH01", (from, msg) => {
    console.log("MSG", from, " saying ", msg);
  });
});

http.listen(3000, () => {
  console.log("listening on *:3000");
});

to listen for the 'CH01' event with on`.

We listen for the connection event with on.

And we get the values from emit on client side from the parameters.

Categories
JavaScript Answers

How to stop Node.js Express app that’s started with ‘npm start’?

To stop Node.js Express app that’s started with ‘npm start’, we use the pkill command.

For instance, in package.json, we add

{
  //...
  "scripts": {
    "start": "app.js",
    "stop": "pkill --signal SIGINT myApp"
  }
  //...
}

to add a stop script that runs pkill with the SIGINT signal to stop the myApp app.

We use myApp to kill app.js since we set process.title to 'myApp' in app.js.

Categories
JavaScript Answers

How to convert entity to plain object with JavaScript Sequelize?

To convert entity to plain object with JavaScript Sequelize, we set the raw option to true.

For instance, we write

db.Sensors.findAll({
  where: {
    nodeId,
  },
  raw: true,
  nest: true,
}).success((sensors) => {
  console.log(sensors);
});

to call findAll to return objects by calling it with the raw option set to true.

We get the returned results from the sensors parameter in the success callback.

Categories
JavaScript Answers

How to add authentication with Socket.IO and JavaScript?

To add authentication with Socket.IO and JavaScript, we call send to get the key from client side and receive it on server side.

For instance, we write

const jwt = require("jsonwebtoken");
//...
app.post("/login", (req, res) => {
  const profile = {
    first_name: "John",
    last_name: "Doe",
    email: "john@doe.com",
    id: 123,
  };

  const token = jwt.sign(profile, jwtSecret, { expiresInMinutes: 60 * 5 });
  res.json({ token });
});

to add the /login endpoint.

Then we call jwt.sign to return am auth token with the profile data.

Then we write

const socketioJwt = require("socketio-jwt");
const sio = socketIo.listen(server);

sio.set(
  "authorization",
  socketioJwt.authorize({
    secret: jwtSecret,
    handshake: true,
  })
);

sio.sockets.on("connection", (socket) => {
  console.log(socket.handshake.decoded_token.email, "has joined");
});

to use the socketio-jwt package to add authentication to Socket.io.

We use it to add JWT authentication with secret set to jwtSecret.

And we listen to the connection event with handler that gets the decoded token’s email.

Categories
JavaScript Answers

How to execute the start script with Nodemon with JavaScript?

To execute the start script with Nodemon with JavaScript, we use the --exec option.

For instance, we run

nodemon --exec npm start

to run npm start with nodemon with the --exec option.