Skip to content

How to understand a Socket.IO codebase

Socket.IO apps communicate by named events instead of URLs. The server listens for `connection`, then registers handlers per event on each socket; clients emit and listen for the same names. Tracing a feature means following event name strings across server and client.

How to tell it's Socket.IO

  • socket.io on the server and socket.io-client in the front end
  • io.on('connection', socket => ...) and socket.on('event', ...)
  • socket.join(room) and io.to(room).emit(...)

Where things usually live

src/socket/index.ts
Creates the server, attaches it to HTTP, registers middleware.
src/socket/handlers/
Event handlers grouped by feature.
src/socket/middleware.ts
io.use auth that runs on connection.
client/src/socket.ts
Client connection, reconnection and event subscriptions.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. Where the Socket.IO server is created and attached
  2. The connection handler
  3. A list of every event name, searched across server and client

How a request moves through the code

  1. 1The client connects; io.use middleware authenticates the handshake.
  2. 2The connection handler registers event listeners and joins rooms.
  3. 3The client emits an event with a payload and optionally an acknowledgement callback.
  4. 4The server handler validates, updates state and emits to a room or all clients.
  5. 5With multiple servers, an adapter such as Redis relays broadcasts between instances.

Questions to ask about the repository

  • List every event the client emits and every event the server emits.
  • Which rooms exist, and when do sockets join and leave them?
  • How is a socket authenticated, and what happens on reconnection?

What trips people up

  • Event names are strings, so typos fail silently; look for shared typed event maps.
  • Without an adapter, broadcasts only reach clients on the same server instance.