Start typing to search...

Dispatch

Implements a dispatch system for real-time communication on the frontend of your Koji app.

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 Koji.dispatch.connect({
 maxConnectionsPerShard: '25',
 authorization: token
});

Source: frontend/dispatch/index.ts#L155

.disconnect()

Disconnects the client from the dispatch shard.

Example

Koji.dispatch.disconnect();

Source: frontend/dispatch/index.ts#L423

.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

Koji.dispatch.emitEvent('myEvent', myDataPayload);

Source: frontend/dispatch/index.ts#L389

.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 { userToken, presumedRole, presumedAttributes  } = await Koji.identity.getToken();
Koji.dispatch.identify(userToken);

Source: frontend/dispatch/index.ts#L371

.info()

Gets information about the active dispatch shards.

Returns

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

Example

const shardInfo = await Koji.dispatch.info();

Source: frontend/dispatch/index.ts#L120

.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 = Koji.dispatch.on('eventName', callbackFunction);

Source: frontend/dispatch/index.ts#L297

.onConnectedClientsChanged(callback)

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]].

Parameters

Returns

Function, Function to unsubscribe from the event listener.

Example

unsubscribeEvent = Koji.dispatch.onConnectedClientsChanged(callbackFunction);

Source: frontend/dispatch/index.ts#L324

.setProjectId(projectId)

Sets the project ID for the dispatch service.

Parameters

  • projectId - String, Unique identifier for the Koji project.

Example

Koji.dispatch.setProjectId(myProject);

Source: frontend/dispatch/index.ts#L135

.setUserInfo(userInfo)

Sets user information that is available in the [[onConnectedClientsChanged]] listener.

Parameters

  • userInfo - [index: string]: any, Data to set for the client's user information.

Example

Koji.dispatch.setUserInfo({ avatar: userAvatar });

Source: frontend/dispatch/index.ts#L356

Interfaces

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 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.

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

ConnectedClientsChangedHandlerCallback:(connectedClients) => void

Function to handle a dispatch event for a new or updated client connection. Invoked by the [[onConnectedClientsChanged]] listener.

Parameters

  • connectedClients - ConnectedClient, Array of information about the connected clients in the shard.

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 - , Object containing additional information about the event, including the message latency in milliseconds.