What is Socket.IO?

Socket.IO is a library that enables real-time, bidirectional, and event-based communication between a client and server.

Key Concepts

- On the client-side, it uses WebSocket (or otherwise, falls back to HTTP long-polling)

- NOT a 100% drop-in replacement for WebSocket, but the API is similar

// client-side
const socket = io('ws://localhost:3000');

socket.on('connect', () => {
  console.log(socket.id); // x8WIv7-mJ...
});

// server-side
io.on('connection', (socket) => {
  console.debug('Someone has connected!');
});

- Provides a Node.js server implementation

I mean, but why?

- Falls back to HTTP long-polling in case the WebSocket connection cannot be established

- Automatic reconnection

- Supports broadcasting to all clients or a subset (aka a 'room')

- Multiplexing aka namespacing

socket.io

By Cameron Sampson

socket.io

  • 106