Start typing to search...

Dispatch

Implements a real-time messaging dispatch system for the backend of your Koji app.

Constructor

Instantiates the Dispatch class.

Parameters

Example

const dispatch = new KojiBackend.Dispatch({ res });

Methods

.connect(config)

Connects a client to a dispatch shard.

Parameters

Returns

Promise<ConnectionInfo>, Connection details, including the client ID and shard name.

Example

const myInfo = await dispatch.connect({
 maxConnectionsPerShard: '25',
 authorization: token
});

Source: backend/dispatch/index.ts#L144

.disconnect()

Disconnects the client from the dispatch shard.

Example

dispatch.disconnect();

Source: backend/dispatch/index.ts#L360

.emitEvent(eventName, payload, recipients)

Emits the named event to the specified recipients or to all clients.

Parameters

  • eventName - String, Name of the event.

  • payload - [index: string]: any, Object of key-value pair data to send as a message payload.

  • recipients - String (Optional), List of clients to receive the event. If this parameter is not included, the event is sent to all clients on the current shard.

Example

dispatch.emitEvent('myEvent', myDataPayload);

Source: backend/dispatch/index.ts#L326

.identify(authToken)

Identifies a connected client, which enables the server and other connected clients to send it secure messages.

Parameters

  • authToken - String, Short-lived user token for the connected client. To get a user token, use {@doclink core-frontend-identity#getToken | Identity.getToken}.

Example

const authToken = await identity.getToken();
dispatch.identify(authToken.token);

Source: backend/dispatch/index.ts#L308

.info()

Gets information about the active dispatch shards.

Returns

Promise<ShardInfo>, Array of objects containing information about the dispatch shards.

Example

const shardInfo = await dispatch.info();

Source: backend/dispatch/index.ts#L124

.on(eventName, callback)

Sets a listener for a specific event, and invokes a callback function when the event is dispatched over the shard.

Parameters

  • eventName - String, Name of the event to subscribe to.

  • callback - MessageHandlerCallback, Function to invoke when the event is fired.

Returns

Function, Function to unsubscribe from the event listener.

Example

unsubscribeEvent = dispatch.on('eventName', callbackFunction);

Source: backend/dispatch/index.ts#L283

Interfaces

BackendConfigurationInput

Configuration information for the Koji app.

Properties

  • projectId - String (Optional), Unique identifier for the Koji app. Will override data passed through `res`.

  • projectToken - String (Optional), Secret key for the Koji app. Will override data passed through `res`.

  • res - Response (Optional), Express response object. Used in conjunction with middleware to scope environment variables for customized versions of the original Koji app. For the original definition see [[https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts#L127 | @types/express]].

ConnectionInfo

Connection details for a client. Returned when the client [[connect | connects to a dispatch shard]].

Properties

  • clientId - String (Optional), ID of the connected client.

  • shardName - String, Name of the dispatch shard that the client is connected to.

DispatchConfigurationInput

Configuration options for a new connection.

Properties

  • authorization - String (Optional), Short-lived user token that identifies the client, so the server and other connected clients can send it secure messages. If the user token is not included, you can [[identify | identify the client]] after it is connected.

  • maxConnectionsPerShard - Number (Optional), Total clients to allow on a shard before it is full (defaults to 100). When a shard is full, new clients are added to a new shard unless a different shard is explicitly set.

  • shardName - String (Optional), Name of the dispatch shard to use. If not specified, the client is added to a shard automatically.

DispatchMessageMetadata

Additional data attached to a dispatch message.

Properties

  • latencyMs - Number, Message latency in milliseconds.

ShardInfo

Information about a dispatch shard.

Properties

  • numConnectedClients - Number, Number of clients currently connected to the dispatch shard.

  • shardName - String, Name of the dispatch shard.

Type aliases

MessageHandlerCallback:(payload, metadata) => void

Function to handle a dispatch event. Invoked by the [[on]] listener.

Parameters

  • payload - [index: string]: any, Data payload sent with the fired event.

  • metadata - DispatchMessageMetadata, Additional data attached to a dispatch message.