NodeJS – Socket.io vs WebSocket

Socket.IO and WebSocket are both libraries for real-time communication between clients and servers, but they have some key differences that make them suitable for different use cases. In this blog, we will compare the two libraries using examples in Node.js.

Socket.IO is a JavaScript library for real-time web applications. It allows bidirectional communication between a client and a server, using WebSockets as the underlying technology. In addition to WebSockets, Socket.IO also uses other techniques such as long-polling and server-sent events to achieve real-time communication, making it more resilient to network issues and able to fall back to older technologies when necessary.

To use Socket.IO in a Node.js server, we can install the socket.io module and set up a Socket.IO server as follows:

const io = require('socket.io')(8080);

io.on('connection', (socket) => {
  console.log('a user connected');

  socket.on('disconnect', () => {
    console.log('user disconnected');
  });

  socket.on('chat message', (msg) => {
    console.log('message: ' + msg);
    io.emit('chat message', msg);
  });
});

This code creates a Socket.IO server on port 8080 and listens for incoming connections. When a new connection is established, the server sets up event listeners for disconnection and incoming messages. When a message is received, the server logs the message and broadcasts it to all connected clients.

To test this server, we can use a client that connects to the server and sends messages. Here is an example of a simple Socket.IO client in Node.js:

const io = require('socket.io-client');
const socket = io('http://localhost:8080');

socket.on('connect', () => {
  console.log('connected to server');
});

socket.on('disconnect', () => {
  console.log('disconnected from server');
});

socket.on('chat message', (msg) => {
  console.log(`received message: ${msg}`);
});

socket.emit('chat message', 'Hello, world!');

This code creates a Socket.IO client and connects to the server at http://localhost:8080. When the connection is established, the client sets up event listeners for disconnection and incoming messages, and sends a message to the server. When a message is received, it is logged to the console.

WebSocket, on the other hand, is a low-level API for establishing full-duplex communication channels over a single TCP connection. It allows for real-time, bi-directional communication between a client and server, but it does not have the same level of fallback support as Socket.IO. If the client or server does not support WebSockets, the connection will not be established.

To use WebSocket in a Node.js server, we can install the ws module and set up a WebSocket server as follows:

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  console.log('a user connected');

  ws.on('close', () => {
    console.log('user disconnected');
  });

This writing was generated using ChatGPT of openai.com

Leave a Comment

Your email address will not be published.

Free Web Hosting