← Back to list

Real-Time Web : Part 2 | Understanding Socket.IO Core Concepts

Thurunu Yasanga Marasinghe · 2026-05-10 13:41 · 0 claps · 1.9 min read
#socketio #websocket #websocket-server
Open on Medium ↗
Wiki topics: 🔒 · Cybersecurity

Real-Time Web : Part 2| Understanding Socket.IO Core Concepts

In our last deep dive, we looked at how Socket.io enables two-way communication. Now let’s get into the building blocks of Socket.io. There are three key concepts every beginner needs to understand: Events, Namespaces, and Rooms.

1. Events: emit and on

Socket.io is built around the concept of events — named signals that either the client or server can send and listen for.

  • **emit** — Send an event (with optional data) to the other side.
  • **on** — Listen for a specific event and react when it arrives.

This is identical in spirit to DOM event listeners in the browser (addEventListener), just over a network.

See how clean that is? You name your events meaningfully ('chat message', 'welcome'), and both sides agree on those names. It reads almost like plain English.

2. Namespaces: Separate Communication Channels

Imagine you’re building a platform that has a chat section and a live dashboard section. You probably don’t want chat events mixing with dashboard events — that would be messy.

Namespaces let you split a single Socket.io connection into multiple isolated channels, each with their own event handlers.

// Server: define separate namespaces
const chatNamespace      = io.of('/chat');
const dashboardNamespace = io.of('/dashboard');

chatNamespace.on('connection', (socket) => {
  // Only chat-related events here
});

dashboardNamespace.on('connection', (socket) => {
  // Only dashboard-related events here
});
// Client: connect to a specific namespace
const chatSocket      = io('/chat');
const dashboardSocket = io('/dashboard');

Think of namespaces like different departments in a company, each with their own internal communication, all running over the same office building (your server).

3. Rooms: Temporary Groups Within a Namespace

Within a namespace, you can organize sockets into Rooms — dynamic, temporary groups.

A real-world example: imagine a multiplayer game where you match players into separate game lobbies. Or a chat app where users can join different topic channels. Rooms are perfect for this.

// Server: a user joins a room
socket.on('join room', (roomName) => {
  socket.join(roomName);
  console.log(`User joined room: ${roomName}`);
});

// Server: send a message to ONLY users in a specific room
io.to('game-lobby-42').emit('game update', { score: 5 });

The beauty: users in 'game-lobby-42' get the update. Everyone else? Unaffected.

Here’s a visual summary of how these three concepts nest together:

In the next chapter, we’ll build a basic real-world project and walk through the codebase together.


메타데이터
post_id
4e943ffd997d
slug
real-time-web-part-2-understanding-socket-io-core-concepts-4e943ffd997d
url
https://medium.com/@marasinghe.ty/real-time-web-part-2-understanding-socket-io-core-concepts-4e943ffd997d
canonical_url
https://medium.com/@marasinghe.ty/real-time-web-part-2-understanding-socket-io-core-concepts-4e943ffd997d
author_url
https://medium.com/@marasinghe.ty
status
ok
fetched_at
2026-07-20 05:33:31