const dispatch = new KojiBackend.Dispatch({ res });
Implements a real-time messaging dispatch system for the backend of your Koji app.
Instantiates the Dispatch class.
config
- BackendConfigurationInput, Configuration information for the Koji app.
const dispatch = new KojiBackend.Dispatch({ res });
Connects a client to a dispatch shard.
config
- DispatchConfigurationInput, Configuration options for a new connection.
Promise<ConnectionInfo>, Connection details, including the client ID and shard name.
const myInfo = await dispatch.connect({
maxConnectionsPerShard: '25',
authorization: token
});
Source: backend/dispatch/index.ts#L144
Disconnects the client from the dispatch shard.
dispatch.disconnect();
Source: backend/dispatch/index.ts#L360
Emits the named event to the specified recipients or to all clients.
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.
dispatch.emitEvent('myEvent', myDataPayload);
Source: backend/dispatch/index.ts#L326
Identifies a connected client, which enables the server and other connected clients to send it secure messages.
authToken
- String, Short-lived user token for the connected client. To get a user token, use {@doclink core-frontend-identity#getToken | Identity.getToken}.
const authToken = await identity.getToken();
dispatch.identify(authToken.token);
Source: backend/dispatch/index.ts#L308
Gets information about the active dispatch shards.
Promise<ShardInfo>, Array of objects containing information about the dispatch shards.
const shardInfo = await dispatch.info();
Source: backend/dispatch/index.ts#L124
Sets a listener for a specific event, and invokes a callback function when the event is dispatched over the shard.
eventName
- String, Name of the event to subscribe to.
callback
- MessageHandlerCallback, Function to invoke when the event is fired.
Function, Function to unsubscribe from the event listener.
unsubscribeEvent = dispatch.on('eventName', callbackFunction);
Source: backend/dispatch/index.ts#L283
Configuration information for the Koji app.
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]].
Connection details for a client. Returned when the client [[connect | connects to a dispatch shard]].
clientId
- String (Optional), ID of the connected client.
shardName
- String, Name of the dispatch shard that the client is connected to.
Configuration options for a new connection.
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.
Additional data attached to a dispatch message.
latencyMs
- Number, Message latency in milliseconds.
Information about a dispatch shard.
numConnectedClients
- Number, Number of clients currently connected to the dispatch shard.
shardName
- String, Name of the dispatch shard.
Function to handle a dispatch event. Invoked by the [[on]] listener.
payload
- [index: string]: any, Data payload sent with the fired event.
metadata
- DispatchMessageMetadata, Additional data attached to a dispatch message.