const myInfo = await Koji.dispatch.connect({
maxConnectionsPerShard: '25',
authorization: token
});
Implements a dispatch system for real-time communication on the frontend of your Koji app.
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 Koji.dispatch.connect({
maxConnectionsPerShard: '25',
authorization: token
});
Source: frontend/dispatch/index.ts#L155
Disconnects the client from the dispatch shard.
Koji.dispatch.disconnect();
Source: frontend/dispatch/index.ts#L423
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.
Koji.dispatch.emitEvent('myEvent', myDataPayload);
Source: frontend/dispatch/index.ts#L389
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 { userToken, presumedRole, presumedAttributes } = await Koji.identity.getToken();
Koji.dispatch.identify(userToken);
Source: frontend/dispatch/index.ts#L371
Gets information about the active dispatch shards.
Promise<ShardInfo>, Array of objects containing information about the dispatch shards.
const shardInfo = await Koji.dispatch.info();
Source: frontend/dispatch/index.ts#L120
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 = Koji.dispatch.on('eventName', callbackFunction);
Source: frontend/dispatch/index.ts#L297
Sets a listener for changes to connected clients, and invokes a callback function when the event is dispatched over the shard. A change occurs whenever any client connects, disconnects, or updates their user information with [[setUserInfo]].
callback
- ConnectedClientsChangedHandlerCallback, Function to invoke when the event is fired.
Function, Function to unsubscribe from the event listener.
unsubscribeEvent = Koji.dispatch.onConnectedClientsChanged(callbackFunction);
Source: frontend/dispatch/index.ts#L324
Sets the project ID for the dispatch service.
projectId
- String, Unique identifier for the Koji project.
Koji.dispatch.setProjectId(myProject);
Source: frontend/dispatch/index.ts#L135
Sets user information that is available in the [[onConnectedClientsChanged]] listener.
userInfo
- [index: string]: any, Data to set for the client's user information.
Koji.dispatch.setUserInfo({ avatar: userAvatar });
Source: frontend/dispatch/index.ts#L356
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 this value 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.
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 for a new or updated client connection. Invoked by the [[onConnectedClientsChanged]] listener.
connectedClients
- ConnectedClient, Array of information about the connected clients in the shard.
Function to handle a dispatch event. Invoked by the [[on]] listener.
payload
- [index: string]: any, Data payload sent with the fired event.
metadata
- , Object containing additional information about the event, including the message latency in milliseconds.