import { Socket } from "https://deno.land/x/socket_io@0.2.0/packages/socket.io/mod.ts";
This is the main object for interacting with a client.
A Socket belongs to a given Namespace and uses an underlying Client to communicate.
Within each Namespace, you can also define arbitrary channels (called "rooms") that the Socket can join and leave. That provides a convenient way to broadcast to a group of socket instances.
Examples
io.on("connection", (socket) => {
console.log(socket ${socket.id} connected
);
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 });
// join the room named "room1" socket.join("room1");
// broadcast to everyone in the room named "room1" io.to("room1").emit("hello");
// upon disconnection
socket.on("disconnect", (reason) => {
console.log(socket ${socket.id} disconnected due to ${reason}
);
});
});
Constructors
Properties
Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the sender.
Additional information that can be attached to the Socket instance and which will be used in the Server.fetchSockets() method.
Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
Methods
Produces an ack callback to emit with an event.
Leave all rooms.
Called upon ack packet.
Called upon client disconnect packet.
Called upon event packet.
Writes a packet.
Makes the socket leave all the rooms it was part of and prevents it from joining any other room
Produces an error
packet.
Notify the listeners for each packet sent (emit or broadcast)
Called upon closing. Called by Client
.
Called by Namespace
upon successful
middleware execution (ie: authorization).
Socket is added to namespace array before
call to join, so adapters can access it.
Disconnects this client.
Emits to this client.
Excludes a room when broadcasting.
Targets a room when broadcasting. Similar to to()
, but might feel clearer in some cases:
Joins a room.
Leaves a room.
Removes the listener that will be fired when any event is received.
Removes the listener that will be fired when any event is sent.
Adds a listener that will be fired when any event is received. The event name is passed as the first argument to the callback.
Adds a listener that will be fired when any event is sent. The event name is passed as the first argument to the callback.
Sends a message
event.
This method mimics the WebSocket.send() method.
Sets a modifier for a subsequent event emission that the callback will be called with an error when the given number of milliseconds have elapsed without an acknowledgement from the client:
Targets a room when broadcasting.