Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 36 ยท Getting Started

What Is Socket.IO?

Learn what Socket.IO is, how it differs from plain HTTP and raw WebSockets, which problems it solves, and when to choose it for chat, dashboards, games and live updates.

Most of the web works like a conversation where only one side may start talking. The browser asks, the server answers, and then the server goes silent until the browser asks again. That is perfect for loading an article, but awkward for a chat message, a live score, a shared whiteboard or a delivery tracker, where the server needs to speak first.

Socket.IO is a JavaScript library that gives the browser and the server a long-lived, two-way channel. Either side can send a named message ("event") at any moment, with any JSON-friendly data attached, and the other side reacts with a handler function. You write socket.emit('chat message', text) on one end and socket.on('chat message', handler) on the other; the library handles connections, retries, buffering and fallbacks.

Socket.IO packages cannot run inside this site's code editors. The real-library snippets are read-only. Every runnable example uses a small hand-written stand-in (labelled in the code) that behaves like Socket.IO for the feature being taught, so you can experiment safely. Install the real package on your own machine to follow along end to end.

The problem: servers cannot talk first

With plain HTTP the server only answers requests. To get live data you have to fake it. The oldest trick is polling: the browser asks "anything new?" every few seconds. It works, but most answers are "no", every request carries headers and cookies, and a message can wait a full interval before you see it.

The simulation below counts the work done by a polling client and by a push channel for the same three events over ten seconds. It uses only Node's EventEmitter.

Node.js Polling versus push (simulation)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Polling made four requests, and two of the three events arrived a second late (with a longer interval the delay grows). The push channel sent exactly three small messages, each at once.

What Socket.IO gives you

Socket.IO is more than a WebSocket wrapper. Its main features:

  • Named events with data. Instead of one stream of text you emit 'chat message', 'user typing' or 'score', with objects, arrays, numbers and even binary data (Buffers, typed arrays) as arguments.
  • Automatic reconnection. If Wi-Fi drops, the client retries with growing delays and random jitter, and the server can even restore missed events in some setups.
  • Fallback transport. It starts with HTTP long-polling and upgrades to WebSocket when possible, so it works behind restrictive proxies and firewalls.
  • Acknowledgements. A sender can pass a callback and get a reply when the other side has processed the event, like a tiny request/response.
  • Rooms and namespaces. Group connections so you can message "everyone in room 42" or split an app into /chat and /admin areas.
  • Broadcasting. Send to everyone, everyone except the sender, or to a group, in one call.
  • Middleware and authentication hooks. Reject unwanted connections during the handshake.
  • Scaling adapters. Share events across many server processes through Redis and other backends.

What Socket.IO adds

Events Named messages with data
Reconnect Automatic retries
Fallback Long-polling if needed
Broadcast Rooms and namespaces

Built on top of WebSocket, not a replacement.

1 / 2

A first look at the code

Here is the shape of a Socket.IO program. This is real-library code, so it is read-only here.

JavaScript
// server.js
import { Server } from 'socket.io';

const io = new Server(3000);

io.on('connection', (socket) => {
  console.log('connected', socket.id);
  socket.on('chat message', (text) => {
    io.emit('chat message', text); // send to everybody
  });
});
JavaScript
// client (browser)
import { io } from 'socket.io-client';

const socket = io('http://localhost:3000');
socket.on('chat message', (text) => console.log('got:', text));
socket.emit('chat message', 'Hello!');

Now the same idea on the stand-in. Two clients connect to a Server; when one emits, the server relays the message to everybody. The code you care about is at the top; the long block below it is the stand-in and you can ignore it.

Node.js A tiny chat with the stand-in

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Both clients receive the message, including the sender, because the server used io.emit, which means "everyone". Later lessons show how to exclude the sender.

Socket.IO is not WebSocket

A common misunderstanding: Socket.IO is not an implementation of the WebSocket protocol. It is its own protocol that usually travels over WebSocket. A plain WebSocket client cannot talk to a Socket.IO server, and the reverse is also true, because Socket.IO adds packet framing, event names, acknowledgement ids and a handshake on top. The next lesson, How Socket.IO Works, shows exactly what those packets look like.

When to use it, and when not to

Choose Socket.IO when you need two-way, low-latency events and want reconnection, rooms and broadcasting without building them: chat, notifications, live dashboards, multiplayer game lobbies, collaborative editors, order tracking.

Consider something simpler when:

  • The server only pushes updates one way. Server-Sent Events are lighter.
  • You need a plain WebSocket to talk to a non-Socket.IO service. Use the browser WebSocket API or the ws package; see WebSockets in Node.js.
  • The data changes every few minutes. Ordinary polling or a refresh button is fine.

If you are new to Node.js servers, skim Node.js events first. Socket.IO's API deliberately mirrors EventEmitter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main limitation of plain HTTP that Socket.IO helps with?

    HTTP is request/response. Socket.IO keeps a channel open so the server can push events whenever it wants.
  2. 2. Can a plain browser WebSocket connect to a Socket.IO server and exchange events?

    Socket.IO adds handshakes, packet types, event names and ack ids, so you need a Socket.IO client.
  3. 3. Which feature lets you send a message to 'everyone in the same group'?

    Rooms group sockets so one call can reach all members.
  4. 4. Which situation fits Server-Sent Events better than Socket.IO?

    When data only flows from server to client, SSE is simpler and lighter.

Summary

  • Socket.IO provides a persistent two-way channel with named events, so servers can push data at any time.
  • It adds reconnection, long-polling fallback, acknowledgements, rooms, namespaces and broadcasting.
  • It has its own protocol; plain WebSocket clients and servers are not compatible with it.
  • Use it for chat, live dashboards, games and collaboration; use SSE or polling when the needs are simpler.
  • The runnable examples in this course use a clearly labelled stand-in because the real packages cannot run in the browser editor.

Next: How Socket.IO Works