Skip to main content
The Deno 2 Release Candidate is here
Learn more
Latest
import { Socket } from "https://deno.land/x/socket_io@0.2.0/packages/socket.io/lib/socket.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);

// 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}); }); });

Type Parameters

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

Properties

private
readonly
adapter: Adapter
private
flags: BroadcastFlags
_acks: Map<number, () => void>

Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the sender.

connected: boolean

Whether the socket is currently connected or not.

data: Partial<SocketData>

Additional information that can be attached to the Socket instance and which will be used in the Server.fetchSockets() method.

readonly
handshake: Handshake

The handshake details.

readonly
id: SocketId

An unique identifier for the session.

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

readonly
rooms: Set<Room>

Returns the rooms the socket is currently in.

readonly
volatile: this

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
ack(id: number): () => void

Produces an ack callback to emit with an event.

private
leaveAll(): void

Leave all rooms.

private
onack(packet: Packet): void

Called upon ack packet.

private
ondisconnect(): void

Called upon client disconnect packet.

private
onevent(packet: Packet): void

Called upon event packet.

private
packet(packet: Omit<Packet, "nsp"> & Partial<Pick<Packet, "nsp">>, opts?): void

Writes a packet.

private
registerAckCallback(id: number, ack: (...args: unknown[]) => void)

Makes the socket leave all the rooms it was part of and prevents it from joining any other room

_error(err: { message: string; data: unknown; })

Produces an error packet.

Notify the listeners for each packet sent (emit or broadcast)

_onclose(reason: DisconnectReason): this | undefined

Called upon closing. Called by Client.

_onconnect(): void

Called by Namespace upon successful middleware execution (ie: authorization). Socket is added to namespace array before call to join, so adapters can access it.

_onpacket(packet: Packet)
disconnect(close?): this

Disconnects this client.

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

Emits to this client.

Excludes a room when broadcasting.

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

join(rooms: Room | Array<Room>): Promise<void> | void

Joins a room.

leave(room: Room): Promise<void> | void

Leaves a room.

offAnyIncoming(listener?: (...args: Event) => void): this

Removes the listener that will be fired when any event is received.

offAnyOutgoing(listener?: (...args: Event) => void): this

Removes the listener that will be fired when any event is sent.

onAnyIncoming(listener: (...args: Event) => void): this

Adds a listener that will be fired when any event is received. The event name is passed as the first argument to the callback.

onAnyOutgoing(listener: (...args: Event) => void): this

Adds a listener that will be fired when any event is sent. The event name is passed as the first argument to the callback.

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

Sends a message event.

This method mimics the WebSocket.send() method.

timeout(timeout: number): this

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.