The properties with which to create the Server.

interface Props {
    acceptNewUsers?: boolean;
    http?: {
        path?: string;
        queryParams?: ExpectedQueryParams;
        server?: Server<typeof IncomingMessage, typeof ServerResponse>;
    };
    isValidUsername?: ((username) => boolean);
    logger?: false | Logger;
    metadata?: boolean;
}

Properties

acceptNewUsers?: boolean

Whether the server should accept new users. If set to false, only users joined via server.join() can join the server.

Default

When not provided, the server will accept new users.
http?: {
    path?: string;
    queryParams?: ExpectedQueryParams;
    server?: Server<typeof IncomingMessage, typeof ServerResponse>;
}

The http configuration that the Server should use.

Type declaration

  • Optional path?: string

    The http path the Server should use for the websocket server.

    Default

    When not provided, the websocket server will use the root path.
    
  • Optional queryParams?: ExpectedQueryParams

    Query string parameters that all connections must provide in their connection url.

    For example: when it is set to { krmx: 'my-server-1-0-2' } a client should connect with 'ws:127.0.0.1:80/example?krmx=my-server-1-0-2'

  • Optional server?: Server<typeof IncomingMessage, typeof ServerResponse>

    The http server that the Server should use.

    Default

    When not provided, a new http server will be created.
    

Default

When not provided, a new http server will be created and the WebSocket server will be on the root path.
isValidUsername?: ((username) => boolean)

A predicate that determines what the server should consider to be a valid username.

Type declaration

    • (username): boolean
    • A predicate that determines what the server should consider to be a valid username.

      Parameters

      • username: string

      Returns boolean

      Default

      When not provided, the server will validate usernames using the following regex: /^[a-z0-9_-]{3,20}$/.
      

Default

When not provided, the server will validate usernames using the following regex: /^[a-z0-9_-]{3,20}$/.
logger?: false | Logger

The logger that the Server should use.

Default

When not provided, it will log (non debug) to the standard console.
metadata?: boolean

Whether metadata should be added to messages send over websocket connections.

If set to true, each message send or broadcast to users will include a 'metadata' field in the root of the json message including the timestamp it was sent and whether the message was a broadcast.

Example of a message that was sent when metadata is set to true: { type: "custom/message", payload: { value: 3 }, metadata: { isBroadcast: false, timestamp: "2023-04-07T19:17:11.432Z" }, }

Default

When not provided, no metadata will be added to messages.