socket.io-client

  • Version 4.4.1
  • Published
  • 977 kB
  • 6 dependencies
  • MIT license

Install

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

Overview

Realtime application framework client

Index

Functions

function connect

connect: {
(opts?: Partial<ManagerOptions & SocketOptions>): Socket;
(uri: string, opts?: Partial<ManagerOptions & SocketOptions>): Socket<
DefaultEventsMap,
DefaultEventsMap
>;
(
uri: string | Partial<ManagerOptions & SocketOptions>,
opts?: Partial<ManagerOptions & SocketOptions>
): Socket<DefaultEventsMap, DefaultEventsMap>;
};
  • Looks up an existing Manager for multiplexing. If the user summons:

    io('http://localhost/a'); io('http://localhost/b');

    We reuse the existing instance based on same scheme/port/host, and we initialize sockets for each namespace.

    Modifiers

    • @public

function io

io: {
(opts?: Partial<ManagerOptions & SocketOptions>): Socket;
(uri: string, opts?: Partial<ManagerOptions & SocketOptions>): Socket<
DefaultEventsMap,
DefaultEventsMap
>;
(
uri: string | Partial<ManagerOptions & SocketOptions>,
opts?: Partial<ManagerOptions & SocketOptions>
): Socket<DefaultEventsMap, DefaultEventsMap>;
};
  • Looks up an existing Manager for multiplexing. If the user summons:

    io('http://localhost/a'); io('http://localhost/b');

    We reuse the existing instance based on same scheme/port/host, and we initialize sockets for each namespace.

    Modifiers

    • @public

function lookup

lookup: {
(opts?: Partial<ManagerOptions & SocketOptions>): Socket;
(uri: string, opts?: Partial<ManagerOptions & SocketOptions>): Socket<
DefaultEventsMap,
DefaultEventsMap
>;
(
uri: string | Partial<ManagerOptions & SocketOptions>,
opts?: Partial<ManagerOptions & SocketOptions>
): Socket<DefaultEventsMap, DefaultEventsMap>;
};
  • Looks up an existing Manager for multiplexing. If the user summons:

    io('http://localhost/a'); io('http://localhost/b');

    We reuse the existing instance based on same scheme/port/host, and we initialize sockets for each namespace.

    Modifiers

    • @public

Classes

class Manager

class Manager<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents
> extends Emitter<{}, {}, ManagerReservedEvents> {}

    constructor

    constructor(opts: Partial<ManagerOptions>);
    • Manager constructor.

      Parameter uri

      engine instance or engine uri/opts

      Parameter opts

      options

      Modifiers

      • @public

    constructor

    constructor(uri?: string, opts?: Partial<ManagerOptions>);

      constructor

      constructor(
      uri?: string | Partial<ManagerOptions>,
      opts?: Partial<ManagerOptions>
      );

        property engine

        engine: Engine;
        • The Engine.IO client instance

          Modifiers

          • @public

        property opts

        opts: Partial<ManagerOptions>;

          method connect

          connect: (fn?: (err?: Error) => void) => this;
          • Alias for open()

            self

            Modifiers

            • @public

          method open

          open: (fn?: (err?: Error) => void) => this;
          • Sets the current transport socket.

            Parameter fn

            optional, callback self

            Modifiers

            • @public

          method randomizationFactor

          randomizationFactor: {
          (v: number): this;
          (): number;
          (v?: number): number | this;
          };
          • Sets the randomization factor

            Parameter v

            the randomization factor self or value

            Modifiers

            • @public

          method reconnection

          reconnection: { (v: boolean): this; (): boolean; (v?: boolean): boolean | this };
          • Sets the reconnection config.

            Parameter v

            true/false if it should automatically reconnect {Manager} self or value

            Modifiers

            • @public

          method reconnectionAttempts

          reconnectionAttempts: {
          (v: number): this;
          (): number;
          (v?: number): number | this;
          };
          • Sets the reconnection attempts config.

            Parameter v

            max reconnection attempts before giving up {Manager} self or value

            Modifiers

            • @public

          method reconnectionDelay

          reconnectionDelay: {
          (v: number): this;
          (): number;
          (v?: number): number | this;
          };
          • Sets the delay between reconnections.

            Parameter v

            delay {Manager} self or value

            Modifiers

            • @public

          method reconnectionDelayMax

          reconnectionDelayMax: {
          (v: number): this;
          (): number;
          (v?: number): number | this;
          };
          • Sets the maximum delay between reconnections.

            Parameter v

            delay self or value

            Modifiers

            • @public

          method socket

          socket: (nsp: string, opts?: Partial<SocketOptions>) => Socket;
          • Creates a new socket for the given nsp.

            {Socket}

            Modifiers

            • @public

          method timeout

          timeout: {
          (v: number | boolean): this;
          (): number | boolean;
          (v?: number | boolean): number | boolean | this;
          };
          • Sets the connection timeout. false to disable

            Parameter v

            connection timeout self or value

            Modifiers

            • @public

          class Socket

          class Socket<
          ListenEvents extends EventsMap = DefaultEventsMap,
          EmitEvents extends EventsMap = ListenEvents
          > extends Emitter<ListenEvents, EmitEvents, SocketReservedEvents> {}

            constructor

            constructor(
            io: Manager<DefaultEventsMap, DefaultEventsMap>,
            nsp: string,
            opts?: Partial<SocketOptions>
            );
            • Socket constructor.

              Modifiers

              • @public

            property active

            readonly active: boolean;
            • Whether the Socket will try to reconnect when its Manager connects or reconnects

            property auth

            auth: { [key: string]: any } | ((cb: (data: object) => void) => void);

              property connected

              connected: boolean;

                property disconnected

                disconnected: boolean;

                  property id

                  id: string;

                    property io

                    readonly io: Manager<ListenEvents, EmitEvents>;

                      property receiveBuffer

                      receiveBuffer: (readonly any[])[];

                        property sendBuffer

                        sendBuffer: Packet[];

                          property volatile

                          readonly volatile: Socket<ListenEvents, EmitEvents>;
                          • Sets a modifier for a subsequent event emission that the event message will be dropped when this socket is not ready to send messages.

                            Returns

                            self

                            Modifiers

                            • @public

                          method close

                          close: () => this;
                          • Alias for disconnect()

                            self

                            Modifiers

                            • @public

                          method compress

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

                            Parameter compress

                            if true, compresses the sending data self

                            Modifiers

                            • @public

                          method connect

                          connect: () => this;
                          • "Opens" the socket.

                            Modifiers

                            • @public

                          method disconnect

                          disconnect: () => this;
                          • Disconnects the socket manually.

                            self

                            Modifiers

                            • @public

                          method emit

                          emit: <Ev extends EventNames<EmitEvents>>(
                          ev: Ev,
                          ...args: EventParams<EmitEvents, Ev>
                          ) => this;
                          • Override emit. If the event is in events, it's emitted normally.

                            self

                            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 open

                          open: () => this;
                          • Alias for connect()

                          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: any[]) => this;
                          • Sends a message event.

                            self

                            Modifiers

                            • @public

                          method timeout

                          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 server:

                            socket.timeout(5000).emit("my-event", (err) => {
                            if (err) {
                            // the server did not acknowledge the event in the given delay
                            }
                            });

                            Returns

                            self

                            Modifiers

                            • @public

                          Interfaces

                          interface ManagerOptions

                          interface ManagerOptions extends EngineOptions {}

                            property autoConnect

                            autoConnect: boolean;
                            • Should we automatically connect? true

                            property forceNew

                            forceNew: boolean;
                            • Should we force a new Manager for this connection? false

                            property multiplex

                            multiplex: boolean;
                            • Should we multiplex our connection (reuse existing Manager) ? true

                            property parser

                            parser: any;
                            • the parser to use. Defaults to an instance of the Parser that ships with socket.io.

                            property path

                            path: string;
                            • The path to get our client file from, in the case of the server serving it '/socket.io'

                            property randomizationFactor

                            randomizationFactor: number;
                            • Used in the exponential backoff jitter when reconnecting 0.5

                            property reconnection

                            reconnection: boolean;
                            • Should we allow reconnections? true

                            property reconnectionAttempts

                            reconnectionAttempts: number;
                            • How many reconnection attempts should we try? Infinity

                            property reconnectionDelay

                            reconnectionDelay: number;
                            • The time delay in milliseconds between reconnection attempts 1000

                            property reconnectionDelayMax

                            reconnectionDelayMax: number;
                            • The max time delay in milliseconds between reconnection attempts 5000

                            property timeout

                            timeout: number;
                            • The timeout in milliseconds for our connection attempt 20000

                            interface SocketOptions

                            interface SocketOptions {}

                              property auth

                              auth:
                              | {
                              [key: string]: any;
                              }
                              | ((cb: (data: object) => void) => void);
                              • the authentication payload sent when connecting to the Namespace

                              Namespaces

                              namespace Socket

                              namespace Socket {}

                                type DisconnectReason

                                type DisconnectReason =
                                | 'io server disconnect'
                                | 'io client disconnect'
                                | 'ping timeout'
                                | 'transport close'
                                | 'transport error';

                                  Package Files (3)

                                  Dependencies (6)

                                  Dev Dependencies (37)

                                  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-client.

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