Skip to main content

Command Palette

Search for a command to run...

SocketIO Overview

Published
3 min read
M

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.

With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.

In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.

I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.

Welcome Back

We've seen that Socket.IO is composed of two main parts:

  1. Server Library: This integrates with the Node.js server.

  2. Client-side Library: This is loaded in the browser.

These live in separate GitHub repositories:

  • socket.io: The backend server library.

  • socket.io-client: The frontend client library.

Socket.IO is versatile and supports multiple languages, such as Java, C++, Dart, and Swift, allowing clients in various languages to communicate with a Socket.IO server.

Client vs. Server

The client and server APIs are designed differently to suit their roles:

  • On the client-side, you always communicate with a single server.

  • On the server-side, you manage multiple clients and can send messages to individual clients or broadcast messages to all clients.

For example, in a chat room:

  • A client sends a message to the server.

  • The server broadcasts the message to all connected clients.

This differs from HTTP requests where the server only responds to the client that made the request. In Socket.IO, the server decides when and how to send messages.

Differences in API

The server API supports additional functionality like:

  • Tracking connected clients.

  • Broadcasting messages to all or specific clients.

Broadcasting Examples

  1. Sending to a single client:

     socket.emit('event_name', data);
    

    Sends a message to the client connected to socket.

  2. Broadcasting to all clients except the sender:

     socket.broadcast.emit('event_name', data);
    

    Useful in a chat application to notify all clients of a new message except the sender.

  3. Broadcasting to all clients including the sender:

     io.emit('event_name', data);
    

    Sends the message to all connected clients.

Advantages of Socket.IO

Socket.IO simplifies WebSocket communication:

  • Its high-level API makes common tasks easier, such as broadcasting messages.

  • It provides fallback mechanisms for clients that don’t support WebSockets.

  • It is built on Node.js’s event emitter pattern, allowing developers to handle events efficiently.

For example:

  • Emitting an event:

      socket.emit('message', 'Hello, client!');
    
  • Broadcasting a message:

      socket.broadcast.emit('message', 'Hello, everyone except the sender!');
    

By contrast, using a lower-level WebSocket library requires managing clients manually and writing additional code to handle broadcasting.

Cheat Sheet

Socket.IO’s documentation includes a cheat sheet with details on emitting events. Here are some common functions:

  • Emit to a single client:

      socket.emit('event', data);
    
  • Broadcast to others:

      socket.broadcast.emit('event', data);
    
  • Broadcast to all clients:

      io.emit('event', data);
    

Building a Pong Game

Let’s apply what we’ve learned to create a multiplayer Pong game. Pong was one of the first video games, released in 1972 by Atari. It’s a simple 2D ping-pong game where players control paddles and try to score by getting the ball past their opponent.

Single-Player Pong

We’ll start by building a single-player Pong game using HTML5 Canvas and JavaScript. The game includes:

  • A paddle controlled by the mouse.

  • A ball that bounces around.

  • A basic AI opponent that follows the ball.

Here’s a preview of the game:

  • The player controls the paddle with the mouse.

  • The AI paddle responds to the ball at a limited speed.

  • Scores are displayed at the top.

Multiplayer Pong

To make the game multiplayer:

  1. Use Socket.IO to connect clients to a server.

  2. Synchronize game state (e.g., ball position, paddle positions) between clients.

  3. Broadcast updates from the server to all connected clients.

This will turn our basic Pong game into a real-time multiplayer experience.

Resources

Let’s dive into the code and start building!