socket.io

  • Version 4.3.1
  • Published
  • 979 kB
  • 6 dependencies
  • MIT license

Install

npm i socket.io
yarn add socket.io
pnpm add socket.io

Overview

node.js realtime framework server

Index

Classes

class BroadcastOperator

class BroadcastOperator<EmitEvents extends EventsMap>
implements TypedEventBroadcaster<EmitEvents> {}

    constructor

    constructor(adapter: any, rooms?: Set<any>, exceptRooms?: Set<any>, flags?: any);

      property local

      readonly local: BroadcastOperator<EmitEvents>;
      • Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.

        a new BroadcastOperator instance

        Modifiers

        • @public

      property volatile

      readonly volatile: BroadcastOperator<EmitEvents>;
      • 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).

        a new BroadcastOperator instance

        Modifiers

        • @public

      method allSockets

      allSockets: () => Promise<Set<SocketId>>;
      • Gets a list of clients.

        Modifiers

        • @public

      method compress

      compress: (compress: boolean) => BroadcastOperator<EmitEvents>;
      • Sets the compress flag.

        Parameter compress

        if true, compresses the sending data a new BroadcastOperator instance

        Modifiers

        • @public

      method disconnectSockets

      disconnectSockets: (close?: boolean) => void;
      • Makes the matching socket instances disconnect

        Parameter close

        whether to close the underlying connection

        Modifiers

        • @public

      method emit

      emit: <Ev extends EventNames<EmitEvents>>(
      ev: Ev,
      ...args: EventParams<EmitEvents, Ev>
      ) => boolean;
      • Emits to all clients.

        Always true

        Modifiers

        • @public

      method except

      except: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
      • Excludes a room when emitting.

        Parameter room

        a new BroadcastOperator instance

        Modifiers

        • @public

      method fetchSockets

      fetchSockets: () => Promise<RemoteSocket<EmitEvents>[]>;
      • Returns the matching socket instances

        Modifiers

        • @public

      method in

      in: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
      • Targets a room when emitting.

        Parameter room

        a new BroadcastOperator instance

        Modifiers

        • @public

      method socketsJoin

      socketsJoin: (room: Room | Room[]) => void;
      • Makes the matching socket instances join the specified rooms

        Parameter room

        Modifiers

        • @public

      method socketsLeave

      socketsLeave: (room: Room | Room[]) => void;
      • Makes the matching socket instances leave the specified rooms

        Parameter room

        Modifiers

        • @public

      method to

      to: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
      • Targets a room when emitting.

        Parameter room

        a new BroadcastOperator instance

        Modifiers

        • @public

      class Namespace

      class Namespace<
      ListenEvents extends EventsMap = DefaultEventsMap,
      EmitEvents extends EventsMap = ListenEvents,
      ServerSideEvents extends EventsMap = DefaultEventsMap
      > extends StrictEventEmitter<
      ServerSideEvents,
      EmitEvents,
      NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents>
      > {}

        constructor

        constructor(
        server: Server<ListenEvents, EmitEvents, ServerSideEvents>,
        name: string
        );
        • Namespace constructor.

          Parameter server

          instance

          Parameter name

        property adapter

        adapter: any;

          property local

          readonly local: BroadcastOperator<EmitEvents>;
          • Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.

            self

            Modifiers

            • @public

          property name

          readonly name: string;

            property server

            readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents>;

            property sockets

            readonly sockets: Map<any, Socket<ListenEvents, EmitEvents, ServerSideEvents>>;

              property volatile

              readonly volatile: BroadcastOperator<EmitEvents>;
              • 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).

                self

                Modifiers

                • @public

              method allSockets

              allSockets: () => Promise<Set<SocketId>>;
              • Gets a list of clients.

                self

                Modifiers

                • @public

              method compress

              compress: (compress: boolean) => BroadcastOperator<EmitEvents>;
              • Sets the compress flag.

                Parameter compress

                if true, compresses the sending data self

                Modifiers

                • @public

              method disconnectSockets

              disconnectSockets: (close?: boolean) => void;
              • Makes the matching socket instances disconnect

                Parameter close

                whether to close the underlying connection

                Modifiers

                • @public

              method emit

              emit: <Ev extends EventNames<EmitEvents>>(
              ev: Ev,
              ...args: EventParams<EmitEvents, Ev>
              ) => boolean;
              • Emits to all clients.

                Always true

                Modifiers

                • @public

              method except

              except: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
              • Excludes a room when emitting.

                Parameter room

                self

                Modifiers

                • @public

              method fetchSockets

              fetchSockets: () => Promise<RemoteSocket<EmitEvents>[]>;
              • Returns the matching socket instances

                Modifiers

                • @public

              method in

              in: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
              • Targets a room when emitting.

                Parameter room

                self

                Modifiers

                • @public

              method send

              send: (...args: EventParams<EmitEvents, 'message'>) => this;
              • Sends a message event to all clients.

                self

                Modifiers

                • @public

              method serverSideEmit

              serverSideEmit: <Ev extends EventNames<ServerSideEvents>>(
              ev: Ev,
              ...args: EventParams<ServerSideEvents, Ev>
              ) => boolean;
              • Emit a packet to other Socket.IO servers

                Parameter ev

                the event name

                Parameter args

                an array of arguments, which may include an acknowledgement callback at the end

                Modifiers

                • @public

              method socketsJoin

              socketsJoin: (room: Room | Room[]) => void;
              • Makes the matching socket instances join the specified rooms

                Parameter room

                Modifiers

                • @public

              method socketsLeave

              socketsLeave: (room: Room | Room[]) => void;
              • Makes the matching socket instances leave the specified rooms

                Parameter room

                Modifiers

                • @public

              method to

              to: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
              • Targets a room when emitting.

                Parameter room

                self

                Modifiers

                • @public

              method use

              use: (
              fn: (
              socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>,
              next: (err?: ExtendedError) => void
              ) => void
              ) => this;
              • Sets up namespace middleware.

                self

                Modifiers

                • @public

              method write

              write: (...args: EventParams<EmitEvents, 'message'>) => this;
              • Sends a message event to all clients.

                self

                Modifiers

                • @public

              class RemoteSocket

              class RemoteSocket<EmitEvents extends EventsMap>
              implements TypedEventBroadcaster<EmitEvents> {}
              • Expose of subset of the attributes and methods of the Socket class

              constructor

              constructor(adapter: any, details: SocketDetails);

                property data

                readonly data: any;

                  property handshake

                  readonly handshake: Handshake;

                    property id

                    readonly id: any;

                      property rooms

                      readonly rooms: Set<any>;

                        method disconnect

                        disconnect: (close?: boolean) => this;
                        • Disconnects this client.

                          Parameter close

                          if true, closes the underlying connection {Socket} self

                          Modifiers

                          • @public

                        method emit

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

                          method join

                          join: (room: Room | Room[]) => void;
                          • Joins a room.

                            Parameter room

                            room or array of rooms

                            Modifiers

                            • @public

                          method leave

                          leave: (room: any) => void;
                          • Leaves a room.

                            Parameter room

                            Modifiers

                            • @public

                          class Server

                          class Server<
                          ListenEvents extends EventsMap = DefaultEventsMap,
                          EmitEvents extends EventsMap = ListenEvents,
                          ServerSideEvents extends EventsMap = DefaultEventsMap
                          > extends StrictEventEmitter<
                          ServerSideEvents,
                          EmitEvents,
                          ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents>
                          > {}

                            constructor

                            constructor(opts?: Partial<ServerOptions>);
                            • Server constructor.

                              Parameter srv

                              http server, port, or options

                              Parameter opts

                              Modifiers

                              • @public

                            constructor

                            constructor(srv?: any, opts?: Partial<ServerOptions>);

                              constructor

                              constructor(srv: any, opts?: Partial<ServerOptions>);

                                property encoder

                                readonly encoder: any;

                                property engine

                                engine: any;
                                • A reference to the underlying Engine.IO server.

                                  Example:

                                  const clientsCount = io.engine.clientsCount;

                                property local

                                readonly local: BroadcastOperator<EmitEvents>;
                                • Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.

                                  self

                                  Modifiers

                                  • @public

                                property sockets

                                readonly sockets: Namespace<ListenEvents, EmitEvents, ServerSideEvents>;

                                  property volatile

                                  readonly volatile: BroadcastOperator<EmitEvents>;
                                  • 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).

                                    self

                                    Modifiers

                                    • @public

                                  method adapter

                                  adapter: { (): AdapterConstructor | undefined; (v: any): this };
                                  • Sets the adapter for rooms.

                                    Parameter v

                                    pathname self when setting or value when getting

                                    Modifiers

                                    • @public

                                  method allSockets

                                  allSockets: () => Promise<Set<SocketId>>;
                                  • Gets a list of socket ids.

                                    Modifiers

                                    • @public

                                  method attach

                                  attach: (srv: http.Server | number, opts?: Partial<ServerOptions>) => this;
                                  • Attaches socket.io to a server or port.

                                    Parameter srv

                                    server or port

                                    Parameter opts

                                    options passed to engine.io self

                                    Modifiers

                                    • @public

                                  method bind

                                  bind: (engine: any) => this;
                                  • Binds socket.io to an engine.io instance.

                                    Parameter engine

                                    engine.io (or compatible) server self

                                    Modifiers

                                    • @public

                                  method close

                                  close: (fn?: (err?: Error) => void) => void;
                                  • Closes server connection

                                    Parameter fn

                                    optional, called as fn([err]) on error OR all conns closed

                                    Modifiers

                                    • @public

                                  method compress

                                  compress: (compress: boolean) => BroadcastOperator<EmitEvents>;
                                  • Sets the compress flag.

                                    Parameter compress

                                    if true, compresses the sending data self

                                    Modifiers

                                    • @public

                                  method connectTimeout

                                  connectTimeout: { (v: number): this; (): number; (v?: number): number | this };
                                  • Set the delay after which a client without namespace is closed

                                    Parameter v

                                    Modifiers

                                    • @public

                                  method disconnectSockets

                                  disconnectSockets: (close?: boolean) => void;
                                  • Makes the matching socket instances disconnect

                                    Parameter close

                                    whether to close the underlying connection

                                    Modifiers

                                    • @public

                                  method except

                                  except: (name: Room | Room[]) => BroadcastOperator<EmitEvents>;
                                  • Excludes a room when emitting.

                                    Parameter name

                                    self

                                    Modifiers

                                    • @public

                                  method fetchSockets

                                  fetchSockets: () => Promise<RemoteSocket<EmitEvents>[]>;
                                  • Returns the matching socket instances

                                    Modifiers

                                    • @public

                                  method in

                                  in: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
                                  • Targets a room when emitting.

                                    Parameter room

                                    self

                                    Modifiers

                                    • @public

                                  method listen

                                  listen: (srv: http.Server | number, opts?: Partial<ServerOptions>) => this;
                                  • Attaches socket.io to a server or port.

                                    Parameter srv

                                    server or port

                                    Parameter opts

                                    options passed to engine.io self

                                    Modifiers

                                    • @public

                                  method of

                                  of: (
                                  name: string | RegExp | ParentNspNameMatchFn,
                                  fn?: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>) => void
                                  ) => Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
                                  • Looks up a namespace.

                                    Parameter name

                                    nsp name

                                    Parameter fn

                                    optional, nsp connection ev handler

                                    Modifiers

                                    • @public

                                  method path

                                  path: { (v: string): this; (): string; (v?: string): string | this };
                                  • Sets the client serving path.

                                    Parameter v

                                    pathname {Server|String} self when setting or value when getting

                                    Modifiers

                                    • @public

                                  method send

                                  send: (...args: EventParams<EmitEvents, 'message'>) => this;
                                  • Sends a message event to all clients.

                                    self

                                    Modifiers

                                    • @public

                                  method serveClient

                                  serveClient: { (v: boolean): this; (): boolean; (v?: boolean): boolean | this };
                                  • Sets/gets whether client code is being served.

                                    Parameter v

                                    whether to serve client code self when setting or value when getting

                                    Modifiers

                                    • @public

                                  method serverSideEmit

                                  serverSideEmit: <Ev extends EventNames<ServerSideEvents>>(
                                  ev: Ev,
                                  ...args: EventParams<ServerSideEvents, Ev>
                                  ) => boolean;
                                  • Emit a packet to other Socket.IO servers

                                    Parameter ev

                                    the event name

                                    Parameter args

                                    an array of arguments, which may include an acknowledgement callback at the end

                                    Modifiers

                                    • @public

                                  method socketsJoin

                                  socketsJoin: (room: Room | Room[]) => void;
                                  • Makes the matching socket instances join the specified rooms

                                    Parameter room

                                    Modifiers

                                    • @public

                                  method socketsLeave

                                  socketsLeave: (room: Room | Room[]) => void;
                                  • Makes the matching socket instances leave the specified rooms

                                    Parameter room

                                    Modifiers

                                    • @public

                                  method to

                                  to: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
                                  • Targets a room when emitting.

                                    Parameter room

                                    self

                                    Modifiers

                                    • @public

                                  method use

                                  use: (
                                  fn: (
                                  socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>,
                                  next: (err?: ExtendedError) => void
                                  ) => void
                                  ) => this;
                                  • Sets up namespace middleware.

                                    self

                                    Modifiers

                                    • @public

                                  method write

                                  write: (...args: EventParams<EmitEvents, 'message'>) => this;
                                  • Sends a message event to all clients.

                                    self

                                    Modifiers

                                    • @public

                                  class Socket

                                  class Socket<
                                  ListenEvents extends EventsMap = DefaultEventsMap,
                                  EmitEvents extends EventsMap = ListenEvents,
                                  ServerSideEvents extends EventsMap = DefaultEventsMap
                                  > extends StrictEventEmitter<ListenEvents, EmitEvents, SocketReservedEventsMap> {}

                                    constructor

                                    constructor(
                                    nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>,
                                    client: Client<ListenEvents, EmitEvents, ServerSideEvents>,
                                    auth: {}
                                    );
                                    • Interface to a Client for a given Namespace.

                                      Parameter nsp

                                      Parameter client

                                      Parameter auth

                                    property broadcast

                                    readonly broadcast: BroadcastOperator<EmitEvents>;
                                    • Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the sender.

                                      {Socket} self

                                      Modifiers

                                      • @public

                                    property client

                                    readonly client: Client<ListenEvents, EmitEvents, ServerSideEvents>;

                                      property conn

                                      readonly conn: any;
                                      • A reference to the underlying Client transport connection (Engine.IO Socket object).

                                        Modifiers

                                        • @public

                                      property connected

                                      connected: boolean;

                                        property data

                                        data: any;
                                        • Additional information that can be attached to the Socket instance and which will be used in the fetchSockets method

                                        property disconnected

                                        disconnected: boolean;

                                          property handshake

                                          readonly handshake: Handshake;

                                            property id

                                            readonly id: any;

                                              property local

                                              readonly local: BroadcastOperator<EmitEvents>;
                                              • Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.

                                                {Socket} self

                                                Modifiers

                                                • @public

                                              property nsp

                                              readonly nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>;

                                                property request

                                                readonly request: any;
                                                • A reference to the request that originated the underlying Engine.IO Socket.

                                                  Modifiers

                                                  • @public

                                                property rooms

                                                readonly rooms: Set<any>;
                                                • Modifiers

                                                  • @public

                                                property volatile

                                                readonly volatile: Socket<ListenEvents, EmitEvents, ServerSideEvents>;
                                                • 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).

                                                  {Socket} self

                                                  Modifiers

                                                  • @public

                                                method compress

                                                compress: (compress: boolean) => this;
                                                • Sets the compress flag.

                                                  Parameter compress

                                                  if true, compresses the sending data {Socket} self

                                                  Modifiers

                                                  • @public

                                                method disconnect

                                                disconnect: (close?: boolean) => this;
                                                • Disconnects this client.

                                                  Parameter close

                                                  if true, closes the underlying connection {Socket} self

                                                  Modifiers

                                                  • @public

                                                method emit

                                                emit: <Ev extends EventNames<EmitEvents>>(
                                                ev: Ev,
                                                ...args: EventParams<EmitEvents, Ev>
                                                ) => boolean;
                                                • Emits to this client.

                                                  Always returns true.

                                                  Modifiers

                                                  • @public

                                                method except

                                                except: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
                                                • Excludes a room when broadcasting.

                                                  Parameter room

                                                  self

                                                  Modifiers

                                                  • @public

                                                method in

                                                in: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
                                                • Targets a room when broadcasting.

                                                  Parameter room

                                                  self

                                                  Modifiers

                                                  • @public

                                                method join

                                                join: (rooms: Room | Array<Room>) => Promise<void> | void;
                                                • Joins a room.

                                                  Parameter rooms

                                                  room or array of rooms a Promise or nothing, depending on the adapter

                                                  Modifiers

                                                  • @public

                                                method leave

                                                leave: (room: string) => Promise<void> | void;
                                                • Leaves a room.

                                                  Parameter room

                                                  a Promise or nothing, depending on the adapter

                                                  Modifiers

                                                  • @public

                                                method listenersAny

                                                listenersAny: () => ((...args: any[]) => void)[];
                                                • Returns an array of listeners that are listening for any event that is specified. This array can be manipulated, e.g. to remove listeners.

                                                  Modifiers

                                                  • @public

                                                method offAny

                                                offAny: (listener?: (...args: any[]) => void) => this;
                                                • Removes the listener that will be fired when any event is emitted.

                                                  Parameter listener

                                                  Modifiers

                                                  • @public

                                                method onAny

                                                onAny: (listener: (...args: any[]) => void) => this;
                                                • Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback.

                                                  Parameter listener

                                                  Modifiers

                                                  • @public

                                                method prependAny

                                                prependAny: (listener: (...args: any[]) => void) => this;
                                                • Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback. The listener is added to the beginning of the listeners array.

                                                  Parameter listener

                                                  Modifiers

                                                  • @public

                                                method send

                                                send: (...args: EventParams<EmitEvents, 'message'>) => this;
                                                • Sends a message event.

                                                  self

                                                  Modifiers

                                                  • @public

                                                method to

                                                to: (room: Room | Room[]) => BroadcastOperator<EmitEvents>;
                                                • Targets a room when broadcasting.

                                                  Parameter room

                                                  self

                                                  Modifiers

                                                  • @public

                                                method use

                                                use: (fn: (event: Array<any>, next: (err?: Error) => void) => void) => this;
                                                • Sets up socket middleware.

                                                  Parameter fn

                                                  middleware function (event, next) {Socket} self

                                                  Modifiers

                                                  • @public

                                                method write

                                                write: (...args: EventParams<EmitEvents, 'message'>) => this;
                                                • Sends a message event.

                                                  self

                                                  Modifiers

                                                  • @public

                                                Interfaces

                                                interface ServerOptions

                                                interface ServerOptions extends EngineOptions, AttachOptions {}

                                                  property adapter

                                                  adapter: AdapterConstructor;
                                                  • the adapter to use the in-memory adapter (https://github.com/socketio/socket.io-adapter)

                                                  property connectTimeout

                                                  connectTimeout: number;
                                                  • how many ms before a client without namespace is closed 45000

                                                  property parser

                                                  parser: any;
                                                  • the parser to use the default parser (https://github.com/socketio/socket.io-parser)

                                                  property path

                                                  path: string;
                                                  • name of the path to capture "/socket.io"

                                                  property serveClient

                                                  serveClient: boolean;
                                                  • whether to serve the client files true

                                                  Package Files (4)

                                                  Dependencies (6)

                                                  Dev Dependencies (13)

                                                  Peer Dependencies (0)

                                                  No peer dependencies.

                                                  Badge

                                                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/socket.io.

                                                  • Markdown
                                                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/socket.io)
                                                  • HTML
                                                    <a href="https://www.jsdocs.io/package/socket.io"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>