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
Type Parameters
Properties
Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
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
Executes the middleware for an incoming client.
Adds a new client
Called when a packet is received from another Socket.IO server
Removes a client. Called by each Socket
.
Makes the matching socket instances disconnect.
Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible Adapter.
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:
Sends a message
event to all clients.
This method mimics the WebSocket.send() method.
Sends a message to the other Socket.IO servers of the cluster.
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.
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.
Registers a middleware, which is a function that gets executed for every incoming Socket.