Skip to content

Instantly share code, notes, and snippets.

@debajyoti-thetaonelab
Last active January 16, 2022 05:43
Show Gist options
  • Select an option

  • Save debajyoti-thetaonelab/1eab8025526e7555207991c8b643e9d6 to your computer and use it in GitHub Desktop.

Select an option

Save debajyoti-thetaonelab/1eab8025526e7555207991c8b643e9d6 to your computer and use it in GitHub Desktop.
Explore socket.io API - 1

Usage

  1. Open 2 terminal
  2. On the first one install dependencies & run the experiment as follows
npm install socket.io
npm install socket.io-client

node experiment_socket.io.js
  1. Note down the PID as printed on the console.
  2. On the second terminal send SIGUSR1, SIGUSR2 & SIGINIT signal to the running node process
KILL -INT <PID>
KILL -USR1 <PID>
KILL -USR2 <PID>

P.S: substitute <PID> with the actual process id of the running node process.

const { Server } = require("socket.io");
const serverIO = new Server();
serverIO.on("connection", (socket) => {
console.log(`Client connected. socket-id: ${socket.id}`);
});
serverIO.on("disconnected", (reason) => {
console.log(`Client disconnected. reason: ${reason}`);
});
serverIO.listen(4100);
const { io } = require("socket.io-client");
const socket = io("http://localhost:4100");
socket.on("connect", () => {
console.log(`Server connected. socket-id: ${socket.id}`);
});
socket.on("disconnect", (reason) => {
console.log(`Server disconnected. reason: ${reason}`);
});
socket.on("TEST_EVENT", function () {
console.log("this = ", this);
});
process.on("SIGUSR1", (signal) => {
console.log("Received signal:", signal);
serverIO.close((err) => {
if (err) {
console.log(err);
}
});
});
process.on("SIGUSR2", (signal) => {
console.log("Received signal:", signal);
socket.close();
});
process.on("SIGINT", (signal) => {
console.log("Received signal:", signal);
serverIO.emit("TEST_EVENT", { id: Math.random() });
return false;
});
console.log("PID:", process.pid, "PPID:", process.ppid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment