Skip to main content
The Deno 2 Release Candidate is here
Learn more
Module

x/socket_io/mod.ts>Server

Socket.IO server for Deno
Latest
class Server
extends EventEmitter<ListenEvents, EmitEvents, ServerReservedEvents<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>
import { Server } from "https://deno.land/x/socket_io@0.2.0/mod.ts";

Represents a Socket.IO server.

Examples

const io = new Server();

io.on("connection", (socket) => { console.log(socket ${socket.id} connected);

// send an event to the client socket.emit("foo", "bar");

socket.on("foobar", () => { // an event was received from the client });

// upon disconnection socket.on("disconnect", (reason) => { console.log(socket ${socket.id} disconnected due to ${reason}); }); });

await serve(io.handler(), { port: 3000, });

Constructors

new
Server(opts?: Partial<ServerOptions & EngineOptions>)

Type Parameters

optional
ListenEvents extends EventsMap = DefaultEventsMap
optional
EmitEvents extends EventsMap = ListenEvents
optional
ServerSideEvents extends EventsMap = DefaultEventsMap
optional
SocketData = unknown

Properties

private
parentNsps: Map<ParentNspNameMatchFn, ParentNamespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>
readonly
_encoder: Encoder
readonly
engine: Engine
readonly
local: BroadcastOperator<EmitEvents, SocketData>

Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.

readonly
opts: ServerOptions
readonly
volatile: BroadcastOperator<EmitEvents, SocketData>

Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to receive messages (because of network slowness or other issues, or because they’re connected through long polling and is in the middle of a request-response cycle).

Methods

_checkNamespace(name: string, auth: Record<string, unknown>): Promise<void>

Executes the middleware for an incoming namespace not already created on the server.

Closes the server.

disconnectSockets(close?): void

Makes the matching socket instances disconnect.

Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.

emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean

Emits to all connected clients.

except(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>

Excludes a room when emitting.

fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>

Returns the matching socket instances.

Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.

handler(additionalHandler?: Handler)

Returns a request handler.

in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>

Targets a room when emitting. Similar to to(), but might feel clearer in some cases:

of(name: string | RegExp | ParentNspNameMatchFn): Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>

Looks up a namespace.

send(...args: EventParams<EmitEvents, "message">): this

Sends a message event to all clients.

This method mimics the WebSocket.send() method.

serverSideEmit<Ev extends EventNames<ServerSideEvents>>(ev: Ev, ...args: EventParams<ServerSideEvents, Ev>): boolean

Sends a message to the other Socket.IO servers of the cluster.

socketsJoin(room: Room | Room[]): void

Makes the matching socket instances join the specified rooms.

Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.

socketsLeave(room: Room | Room[]): void

Makes the matching socket instances leave the specified rooms.

Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.

timeout(timeout: number): BroadcastOperator<EmitEvents, SocketData>

Adds a timeout in milliseconds for the next operation.

to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>

Targets a room when emitting.

use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => Promise<void>): this

Registers a middleware, which is a function that gets executed for every incoming Socket.