Server is a websocket server that abstracts individual websocket connections into users.

interface Server {
    name: string;
    all(listener): Unsubscribe;
    broadcast<TMessage>(message, skipUsername?): void;
    close(): Promise<void>;
    getStatus(): Status;
    getUsers(): User[];
    join(username): void;
    kick(username): void;
    listen(port?): Promise<number>;
    on<K>(eventName, listener): Unsubscribe;
    once<K>(eventName, listener, predicate?): Unsubscribe;
    pipe<NextEventMap>(configureTap): EventEmitter<NextEventMap>;
    send<TMessage>(username, message): void;
    unlink(username): void;
    waitFor<K>(eventName, predicate?): Promise<Events[K]>;
}

Hierarchy

Properties

name: string

The name of the event emitter. It will be used in the output logs when errors occur in the event listeners.

Methods

  • Start listening to all events. The provided listener will be invoked each time any event is emitted.

    Parameters

    • listener: AllEventListener<keyof Events, void>

      The callback that will be invoked when any event is emitted. It will be invoked with the event name and the event information in its arguments.

    Returns Unsubscribe

  • Broadcast a message to all users, that are currently connected to the server.

    Type Parameters

    • TMessage extends Message

    Parameters

    • message: TMessage

      The message to send to all the users.

    • Optional skipUsername: string

      (optional) The username of the user you want to skip sending this message to.

    Returns void

  • Close the server. This function is asynchronous, the server is closed when all connections are ended and the server emits a 'close' event.

    Returns Promise<void>

  • Get the current users known to the server.

    Returns User[]

  • Join a new (unlinked) user to the server.

    Parameters

    • username: string

      The username of the user to join to the server.

    Returns void

  • Kick a user from the server.

    Parameters

    • username: string

      The username of the user to kick from the server.

    Returns void

  • Start listening at the specified port number. This function is asynchronous, the server is listening once the server emits a 'listen' event.

    Note: If you provided a http server that is already listening, then you can leave the port undefined or use the exact same port number. If you provide a different port number, this method will throw an error.

    Parameters

    • Optional port: number

      The port number at which to start the server. When not provided, the server will start listening at an available port.

    Returns Promise<number>

  • Start listening to a specific event. The provided listener will be invoked each time the event is emitted.

    Type Parameters

    Parameters

    • eventName: K

      The name of the event to listen to.

    • listener: EventListener<Events[K], void>

      The callback that will be invoked when the event is emitted. It will be invoked with the event information in its arguments.

    Returns Unsubscribe

  • Start listening to a specific event only once. The provided listener will be invoked the first time the event is emitted.

    If a predicate is provided, the listener will only be invoked if the predicate returns true. The listener will be removed after the first time it is invoked. If the predicate returns false, the event subscription will be kept intact, until the predicate returns true.

    Type Parameters

    Parameters

    • eventName: K

      The name of the event to listen to.

    • listener: EventListener<Events[K], void>

      The callback that will be invoked when the event is emitted. It will be invoked with the event information in its arguments.

    • Optional predicate: EventListener<Events[K], boolean>

      An optional predicate that can be used to filter out events. If the predicate returns true, the listener will be invoked.

    Returns Unsubscribe

  • Create new event emitter based on the existing event emitter with custom logic to define which events are passed through or transformed.

    Type Parameters

    • NextEventMap extends Record<string, unknown[]>

    Parameters

    • configureTap: ((tap) => void)

      In the configure tap callback function you can create custom logic to pass or transform events to

      Example:

      type MyEventMap = { 'hello': [name: string], 'one': [], 'unused': [value: number] }
      const myEmitter: EventEmitter<MyEventMap> = ...;
      type MyPipedEventMap = { 'hello': [name: string], 'two': [] }
      const myPipedEmitter = myEmitter.pipe<MyPipedEventMap>(pipe => {
      pipe.pass('hello');
      pipe.on('one' () => {
      pipe.emit('two');
      });
      });
      myPipedEmitter.on('hello', (name: string) => { ... })
      myPipedEmitter.on('two', () => { ... })

      TODO: Ensure that the pipe has a similar unsubscribe interface as on and once.
        • (tap): void
        • Parameters

          • tap: {
                emit<NextK>(eventName, ...args): unknown[];
                on<PrevK>(eventName, listener): void;
                pass<PrevK, MutualEventKey>(...eventNames): void;
            }
            • emit:function
              • Emits an event on the pipe to be received by the new EventEmitter.

                Type Parameters

                • NextK extends string | number | symbol

                Parameters

                • eventName: NextK

                  The name of the event to emit.

                • Rest ...args: NextEventMap[NextK]

                  The arguments of the event to emit.

                Returns unknown[]

            • on:function
              • Start listening to a specific event on the source event emitter.

                Type Parameters

                Parameters

                • eventName: PrevK

                  The name of the event to listen to.

                • listener: EventListener<Events[PrevK], void>

                  The callback that will be invoked when the event is emitted. It will be invoked with the event information in its arguments.

                Returns void

            • pass:function
              • Ensures that events (with the provided event names) are passed through as-is to the next event emitter. Note: Only event names that exist on both sides of the pipe and that have the same argument array signature are allowed to be passed.

                Type Parameters

                • PrevK extends keyof Events = keyof Events
                • MutualEventKey extends "listen" | "close" | "authenticate" | "join" | "link" | "unlink" | "leave" | "message" = PrevK extends keyof NextEventMap
                      ? NextEventMap[PrevK] extends Events[PrevK]
                          ? PrevK
                          : never
                      : never

                Parameters

                • Rest ...eventNames: MutualEventKey[]

                  The names of the events you want to pass through as-is to the next event emitter. Only events that exist on both sides of the pipe and that have the same argument array signature are allowed to be passed.

                Returns void

          Returns void

    Returns EventEmitter<NextEventMap>

  • Send a message to a specific user.

    Type Parameters

    • TMessage extends Message

    Parameters

    • username: string

      The username of the user to send the message to.

    • message: TMessage

      The message to send to the user.

    Returns void

  • Unlink the connection of a user from the server.

    Parameters

    • username: string

      The username of the user to unlink from its connection.

    Returns void

  • Wait for a specific event. The returned promise will resolve when the event is emitted. The promise will resolve with the event information.

    Type Parameters

    Parameters

    • eventName: K

      The name of the event to wait for.

    • Optional predicate: EventListener<Events[K], boolean>

      Optionally provides a predicate that is invoked with every occurrence of the event. The promise will only resolve once the predicate returns true. It will be invoked with the event information in its arguments. If the predicate throws an error, the promise will be rejected with that error.

    Returns Promise<Events[K]>