Node.js - Socket.IO support

Updated

Introduction

Socket.IO is a higher-level real-time communication framework that is often mentioned alongside WebSocket. It can use WebSocket as a transport, yet it adds its own protocol, message model, reconnection behavior, and fallback mechanisms. This extension supports the following Socket.IO libraries:

Objects

Icon Metamodel name Description
TypeScript WebSocket Server Represents a WebSocket server endpoint identified by its path
TypeScript WebSocket Client Represents a WebSocket client connection identified by its URL
TypeScript Unknown WebSocket Client Represents a WebSocket client whose URL cannot be statically resolved

A WebSocketServer with name {} is created when the server path cannot be statically determined (for example when the path is passed as a runtime parameter). A server with path {} is treated as a catch-all endpoint.

Link type From To Created when
callLink TypeScript WebSocket Server TypeScript Function / Method A handler is registered on the server, a namespace (io.of('/ns')) or a connected socket: on, once, addListener,prependListener, prependOnceListener, onAny, prependAny, onAnyOutgoing, prependAnyOutgoing, use (middleware). Also NestJS gateways: @SubscribeMessage methods and the handleConnection, handleDisconnect and afterInit hooks
callLink TypeScript Function / Method / Module TypeScript WebSocket Server A function sends from the server side: emit, send, write, emitWithAck, serverSideEmit or serverSideEmitWithAck on the server, a namespace or a connected socket. This includes chained forms such as io.to(room).emit,socket.broadcast.emit and socket.timeout(ms).emit, and this.server.emit in a NestJS gateway.
callLink TypeScript Function / Method / Module TypeScript WebSocket Client / Unknown WebSocket Client A function opens the connection (io(url), io.connect(url), new Manager(url).socket('/ns')) or sends a message on it (emit, send, emitWithAck, …).
callLink TypeScript WebSocket Client / Unknown WebSocket Client TypeScript Function / Method A handler is registered on the client socket: on,once, addListener, prependListener, prependOnceListener, onAny, prependAny,onAnyOutgoing, prependAnyOutgoing

Supported API methods

Server side

API method Object created / linked Link type
new Server(httpServer, opts), new socketio.Server(...), socketIO(httpServer) TypeScript WebSocket Server / (main namespace) is created —
io.of('/admin') TypeScript WebSocket Server named after the namespace (/admin) is created —
io.of(/regex/), io.of((name, auth, next) => ...) TypeScript WebSocket Server {} (dynamic namespace) is created —
io.on('connection' | 'connect', handler), io.sockets.on(...), nsp.on(...) WebSocket Server → handler callLink
io.use(middleware), nsp.use(middleware) WebSocket Server → middleware callLink
socket.on(event, handler), once, addListener, prependListener, prependOnceListener (connected socket) WebSocket Server of the socket’s namespace → handler callLink
socket.onAny(handler), prependAny, onAnyOutgoing, prependAnyOutgoing (connected socket) WebSocket Server of the socket’s namespace → handler callLink
io.emit(...), nsp.emit(...), io.to(room).emit(...), io.in(room).emit(...), io.except(room).emit(...) Calling Function / Method / Module → WebSocket Server callLink
io.serverSideEmit(...), io.serverSideEmitWithAck(...) Calling Function / Method / Module → WebSocket Server callLink
socket.emit(...), socket.send(...), socket.write(...), socket.emitWithAck(...) (connected socket) Calling Function / Method / Module → WebSocket Server of the socket’s namespace callLink
socket.broadcast.emit(...), socket.to(room).emit(...), socket.timeout(ms).emit(...), socket.volatile.emit(...) Calling Function / Method / Module → WebSocket Server of the socket’s namespace callLink

Client side

API method Object created / linked Link type
io('http://host:3000/admin'), io.connect(url), ioc(url) (any function imported from socket.io-client) TypeScript WebSocket Client named after the URL, whose path is the namespace (http://host:3000 → http://host:3000/) is created —
io(), io({ auth, ... }) (no URL: same origin, main namespace) TypeScript WebSocket Client / is created —
io(url) with a URL that can’t be resolved TypeScript Unknown WebSocket Client is created —
new Manager(url).socket('/ns'), manager.socket('/ns') TypeScript WebSocket Client named <scheme>://<host>/ns (/ns if the Manager has no URL) is created —
Any of the calls above that opens a connection Openin Function / Method / Module → WebSocket Client callLink
socket.on(event, handler), once, addListener, prependListener, prependOnceListener WebSocket Client → handler callLink
socket.onAny(handler), prependAny, onAnyOutgoing, prependAnyOutgoing WebSocket Client → handler callLink
socket.io.on(event, handler) (Manager events: reconnect, error, …) WebSocket Client → handler callLink
socket.emit(...), socket.send(...), socket.write(...), socket.emitWithAck(...) Calling Function / Method / Module → WebSocket Client callLink
socket.timeout(ms).emit(...), socket.volatile.emit(...), socket.compress(false).emit(...) Calling Function / Method / Module → WebSocket Client callLink

Example

When analyzing the following files

server.ts

import { createServer } from 'http';
import { Server, Socket } from 'socket.io';

const httpServer = createServer();
const io = new Server(httpServer, { cors: { origin: '*' } });
const chat = io.of('/chat');

function onJoin(room: string) {}
function onChatMessage(text: string) {}

function onChatConnection(socket: Socket) {
    socket.on('join', onJoin);
    socket.on('message', onChatMessage);
}
chat.on('connection', onChatConnection);

export function broadcastMessage(text: string) {
    chat.emit('message', text);
}

httpServer.listen(3000);

client.ts

import { io, Socket } from 'socket.io-client';

const socket: Socket = io('http://localhost:3000/chat');

function onConnect() {}
function onMessage(text: string) {}
socket.on('connect', onConnect);
socket.on('message', onMessage);

export function join(room: string) {
    socket.emit('join', room);
}

export function sendMessage(text: string) {
    socket.emit('message', text);
}

you will get the following result: