import { Namespace, type ExtendedError, type ServerReservedEventsMap } from './namespace'; import { BroadcastOperator } from './broadcast'; import { Adapter, type Room } from './socket.io-adapter'; import type { Server as BunServer, ServerWebSocket } from 'bun'; import type { DisconnectReason, DefaultSocketData } from '../types/socket-types'; import { StrictEventEmitter, type EventsMap, type DefaultEventsMap, type EventParams, type RemoveAcknowledgements, type Empty } from '../types/typed-events'; import { Socket } from './socket'; import type { Context } from 'hono'; import { Client } from './client'; import * as parser from './socket.io-parser'; import type { Encoder } from './socket.io-parser'; import { Server as Engine } from './engine.io'; import type { WSContext, WSEvents } from 'hono/ws'; type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter); export interface ServerOptions { /** * name of the path to capture * @default "/socket.io" */ path: string; /** * the adapter to use * @default the in-memory adapter (@/adapter) */ adapter: AdapterConstructor; /** * the parser to use * @default the default parser (@/socket.io-parser) */ parser: typeof parser; /** * how many ms before a client without namespace is closed * @default 45000 */ connectTimeout: number; /** * Whether to enable the recovery of connection state when a client temporarily disconnects. * * The connection state includes the missed packets, the rooms the socket was in and the `data` attribute. */ connectionStateRecovery: { /** * The backup duration of the sessions and the packets. * * @default 120000 (2 minutes) */ maxDisconnectionDuration?: number; /** * Whether to skip middlewares upon successful connection state recovery. * * @default true */ skipMiddlewares?: boolean; }; /** * Whether to remove child namespaces that have no sockets connected to them * @default false */ cleanupEmptyChildNamespaces: boolean; pingTimeout: number; pingInterval: number; transports: 'websocket'; perMessageDeflate: boolean; maxPayload: number; } /** * Represents a Socket.IO server. * * @example * import { Server } from "socket.io"; * * 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}`); * }); * }); * * io.listen(3000); */ declare class Server< /** * Types for the events received from the clients. * * @example * interface ClientToServerEvents { * hello: (arg: string) => void; * } * * const io = new Server(); * * io.on("connection", (socket) => { * socket.on("hello", (arg) => { * // `arg` is inferred as string * }); * }); */ ListenEvents extends EventsMap = DefaultEventsMap | Empty, /** * Types for the events sent to the clients. * * @example * interface ServerToClientEvents { * hello: (arg: string) => void; * } * * const io = new Server(); * * io.emit("hello", "world"); */ EmitEvents extends EventsMap = ListenEvents | Empty, /** * Types for the events received from and sent to the other servers. * * @example * interface InterServerEvents { * ping: (arg: number) => void; * } * * const io = new Server(); * * io.serverSideEmit("ping", 123); * * io.on("ping", (arg) => { * // `arg` is inferred as number * }); */ ServerSideEvents extends EventsMap = DefaultEventsMap | Empty, /** * Additional properties that can be attached to the socket instance. * * Note: any property can be attached directly to the socket instance (`socket.foo = "bar"`), but the `data` object * will be included when calling {@link Server#fetchSockets}. * * @example * io.on("connection", (socket) => { * socket.data.eventsCount = 0; * * socket.onAny(() => { * socket.data.eventsCount++; * }); * }); */ SocketData extends DefaultSocketData = DefaultSocketData> extends StrictEventEmitter< /** strict typing */ ServerSideEvents, RemoveAcknowledgements, ServerReservedEventsMap< /** strict typing */ ListenEvents, EmitEvents, ServerSideEvents, SocketData>> { readonly sockets: Namespace; /** * A reference to the underlying Engine.IO server. * * @example * const clientsCount = io.engine.clientsCount; * */ engine: Engine; bun: BunServer; /** @private */ readonly _parser: typeof parser; /** @private */ readonly encoder: Encoder; /** @private */ readonly _nsps: Map>; private parentNsps; /** * A subset of the {@link parentNsps} map, only containing {@link ParentNamespace} which are based on a regular * expression. * * @private */ private parentNamespacesFromRegExp; readonly _clients: Map>; private _adapter?; private readonly opts; private _path?; /** @private */ _connectTimeout: number; /** * Server constructor. * * @param [opts] */ constructor(opts?: Partial); get _opts(): Partial; /** * Executes the middleware for an incoming namespace not already created on the server. * * @param name - name of incoming namespace * @param auth - the auth parameters * @param fn - callback * * @private */ _checkNamespace(name: string, auth: { [key: string]: any; }, fn: (nsp: Namespace | false) => void): void; /** * Sets the client serving path. * * @param {String} v pathname * @return {Server|String} self when setting or value when getting */ path(v: string): this; path(): string; path(v?: string): this | string; /** * Set the delay after which a client without namespace is closed * @param v */ connectTimeout(v: number): this; connectTimeout(): number; connectTimeout(v?: number): this | number; /** * Sets the adapter for rooms. * * @param v pathname * @return self when setting or value when getting */ adapter(): AdapterConstructor | undefined; adapter(v: AdapterConstructor): this; /** * Attaches socket.io to a bun server. * * @param srv - server instance * @param opts - options passed to engine.io * @return self */ listen(srv: BunServer, opts?: Partial): void; /** * Attaches socket.io to a bun server. * Initialize engine server. * * @param srv - server instance * @param opts - options passed to engine.io * @return self */ attach(srv: BunServer, opts?: Partial): void; /** * Initialize engine.io and bind socket.io to it * * @param srv - the server to attach to * @param opts - options passed to engine.io * @private */ private initEngine; /** * Binds socket.io to an engine.io instance. * * @param engine engine.io (or compatible) server * @return self */ bind(engine: any): this; /** * Called with each incoming transport connection. * * @param {Context} c * @param {SocketData} data * @return any (for compatibility) * @private */ onconnection(c: Context, data?: SocketData): WSEvents>; /** * Looks up a namespace. * * @example * // with a simple string * const myNamespace = io.of("/my-namespace"); * * // with a regex * const dynamicNsp = io.of(/^\/dynamic-\d+$/).on("connection", (socket) => { * const namespace = socket.nsp; // newNamespace.name === "/dynamic-101" * * // broadcast to all clients in the given sub-namespace * namespace.emit("hello"); * }); * * @param name - nsp name * @param fn optional, nsp `connection` ev handler */ of( /** namespace */ name: string | RegExp, fn?: (socket: Socket) => void): Namespace; /** * Closes server connection * * @param [fn] optional, called as `fn([err])` on error OR all conns closed */ close(fn?: (err?: Error) => void): Promise; /** * Registers a middleware, which is a function that gets executed for every incoming {@link Socket}. * * @example * io.use((socket, next) => { * // ... * next(); * }); * * @param fn - the middleware function */ use(fn: ( /** strict types */ socket: Socket, next: (err?: ExtendedError) => void) => void): this; /** * Targets a room when emitting. * * @example * // the “foo” event will be broadcast to all connected clients in the “room-101” room * io.to("room-101").emit("foo", "bar"); * * // with an array of rooms (a client will be notified at most once) * io.to(["room-101", "room-102"]).emit("foo", "bar"); * * // with multiple chained calls * io.to("room-101").to("room-102").emit("foo", "bar"); * * @param room - a room, or an array of rooms * @return a new {@link BroadcastOperator} instance for chaining */ to(room: Room | Room[]): BroadcastOperator; /** * Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases: * * @example * // disconnect all clients in the "room-101" room * io.in("room-101").disconnectSockets(); * * @param room - a room, or an array of rooms * @return a new {@link BroadcastOperator} instance for chaining */ in(room: Room | Room[]): BroadcastOperator; /** * Excludes a room when emitting. * * @example * // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room * io.except("room-101").emit("foo", "bar"); * * // with an array of rooms * io.except(["room-101", "room-102"]).emit("foo", "bar"); * * // with multiple chained calls * io.except("room-101").except("room-102").emit("foo", "bar"); * * @param room - a room, or an array of rooms * @return a new {@link BroadcastOperator} instance for chaining */ except(room: Room | Room[]): BroadcastOperator; /** * Sends a `message` event to all clients. * * This method mimics the WebSocket.send() method. * * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send * * @example * io.send("hello"); * * // this is equivalent to * io.emit("message", "hello"); * * @return self */ send(...args: EventParams): this; /** * Sends a `message` event to all clients. Alias of {@link send}. * * @return self */ write(...args: EventParams): this; private serverSideEmit; private serverSideEmitWithAck; /** * Sets the compress flag. * * @example * io.compress(false).emit("hello"); * * @param compress - if `true`, compresses the sending data * @return a new {@link BroadcastOperator} instance for chaining */ compress(compress: boolean): BroadcastOperator, 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). * * @example * io.volatile.emit("hello"); // the clients may or may not receive it * * @return a new {@link BroadcastOperator} instance for chaining */ get volatile(): BroadcastOperator, SocketData>; /** * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node. * * @example * // the “foo” event will be broadcast to all connected clients on this node * io.local.emit("foo", "bar"); * * @return a new {@link BroadcastOperator} instance for chaining */ get local(): BroadcastOperator, SocketData>; /** * Adds a timeout in milliseconds for the next operation. * * @example * io.timeout(1000).emit("some-event", (err, responses) => { * if (err) { * // some clients did not acknowledge the event in the given delay * } else { * console.log(responses); // one response per client * } * }); * * @param timeout */ timeout(timeout: number): BroadcastOperator>, SocketData>; /** * Returns the matching socket instances. * * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}. * * @example * // return all Socket instances * const sockets = await io.fetchSockets(); * * // return all Socket instances in the "room1" room * const sockets = await io.in("room1").fetchSockets(); * * for (const socket of sockets) { * console.log(socket.id); * console.log(socket.handshake); * console.log(socket.rooms); * console.log(socket.data); * * socket.emit("hello"); * socket.join("room1"); * socket.leave("room2"); * socket.disconnect(); * } */ fetchSockets(): Promise; /** * 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 {@link Adapter}. * * @example * * // make all socket instances join the "room1" room * io.socketsJoin("room1"); * * // make all socket instances in the "room1" room join the "room2" and "room3" rooms * io.in("room1").socketsJoin(["room2", "room3"]); * * @param room - a room, or an array of rooms */ socketsJoin(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 {@link Adapter}. * * @example * // make all socket instances leave the "room1" room * io.socketsLeave("room1"); * * // make all socket instances in the "room1" room leave the "room2" and "room3" rooms * io.in("room1").socketsLeave(["room2", "room3"]); * * @param room - a room, or an array of rooms */ socketsLeave(room: Room | Room[]): void; /** * Makes the matching socket instances disconnect. * * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}. * * @example * // make all socket instances disconnect (the connections might be kept alive for other namespaces) * io.disconnectSockets(); * * // make all socket instances in the "room1" room disconnect and close the underlying connections * io.in("room1").disconnectSockets(true); * * @param close - whether to close the underlying connection */ disconnectSockets(close?: boolean): void; } declare const io: ((opts?: any) => Server) & { Server: typeof Server; Namespace: typeof Namespace; Socket: typeof Socket; }; export default io; export { Server, Socket, Namespace, BroadcastOperator }; export type { Empty, DefaultSocketData }; export type { DisconnectReason, DefaultEventsMap, ExtendedError }; export type { Event } from './socket';