Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Latest
import { Namespace } from "https://deno.land/x/socket_io@0.2.0/packages/socket.io/lib/namespace.ts";

A Namespace is a communication channel that allows you to split the logic of your application over a single shared connection.

Each namespace has its own:

  • event handlers
io.of("/orders").on("connection", (socket) => {
  socket.on("order:list", () => {});
  socket.on("order:create", () => {});
});

io.of("/users").on("connection", (socket) => {
  socket.on("user:list", () => {});
});
  • rooms
const orderNamespace = io.of("/orders");

orderNamespace.on("connection", (socket) => {
  socket.join("room1");
  orderNamespace.to("room1").emit("hello");
});

const userNamespace = io.of("/users");

userNamespace.on("connection", (socket) => {
  socket.join("room1"); // distinct from the room in the "orders" namespace
  userNamespace.to("room1").emit("holà");
});
  • middlewares
const orderNamespace = io.of("/orders");

orderNamespace.use(async (socket) => {
  // ensure the socket has access to the "orders" namespace
});

const userNamespace = io.of("/users");

userNamespace.use(async (socket) => {
  // ensure the socket has access to the "users" namespace
});

Constructors

new
Namespace(server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, name: string)

Type Parameters

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

Properties

_fns: Array<(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => Promise<void>>
_ids: number
adapter: Adapter

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

readonly
name: string

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

private
run(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>): Promise<void>

Executes the middleware for an incoming client.

Adds a new client

_onServerSideEmit(args: [string, ...unknown[]])

Called when a packet is received from another Socket.IO server

Removes a client. Called by each Socket.

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.

Excludes a room when emitting.

Returns the matching socket instances.

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

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

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)

Adds a timeout in milliseconds for the next operation.

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.