Skip to main content

Custom Events Library

Custom Events

Provides methods to send and listen to custom events. These events can be sent to a specific user(s) in the channel or to all the user(s) in the channel.

These events can be sent to

  • A specific user in the channel.
  • A set of users in the channel.
  • All the users in the channel.

These events can be sent with different levels of persistance.

  • None:

    These events are short-lived. A "None" event sent by a user will only be available to other users in the room at that time. These events won't be available to users who join the room after the event has been sent.

  • Sender:

    These events are persisted until the sender leaves the channel. A "Sender" event sent by a user will be available to other users in the room regardless of when they join as long as the sender is present in the room. These events won't be available to users who join the room after the sender has left the room.

  • Session:

    These events are persisted until the session ends. A "Session" event sent by a user will be available to other users if there is at least one user present in the room. When the last user leaves a room, the session ends. All events are cleared when a session ends.


Import


customEvents object handles customization api events and provides methods for sending, subscribing and unsubscribing to events.


Methods


send

Sends the event with the provided details.

send : ( eventName: string , payload: string , persistLevel: PersistanceLevel , receiver? : ReceiverUid ) : void

PropTypeDescription
eventNamestringName of the event to be sent
payloadstringPayload to be sent along with the event
persistLevelPersistanceLevelSets the persistence of the event
receiver?ReceiverUidUid(s) to send the message to. Leave emtpy to send as a channel message to all users

on

Subscribes to the event. Use on method to add listener for specific event.

on( eventName: string , listener: EventCallback ) : Unsubscribe

PropTypeDescription
eventNamestringName of the event to be subscribed
listenerFunctionCallback method for the event to be called when event is received

off

Use off method to remove listener for specific event. If no listener is provided, all listeners added on eventName will be removed. If both eventName and listener are not provided, all events will be removed;

Additionally, method on returns unbind function. Call it and this listener will be removed from event.

off(eventName?: string , listener?: Function ) : void

PropTypeDescription
eventName?stringName of the event to be unsubscribed. If no event name provided all subscribed events will be unsubscribed
listener?FunctionMethod to unsubscribe. If no listener is provided, all listeners added on the eventName will be unsubscribed

  1. Removing listener using calling unsubscribe method
  1. Removing a single listener by passing pass eventname and function.
  1. Removing a multiple listener by passing pass eventname and function.
  1. Removing all listeners

Example



Types


PersistanceLevel

enum PersistanceLevel {
"None" = 1,
"Sender",
"Session",
}

ReceiverUid

ReceiverUid: UidType | UidType[]


Unsubscribe

interface Unsubscribe {
(): void;
}

EventCallback

interface EventCallbackPayload {
payload: string;
persistLevel: PersistanceLevel;
sender: UidType;
ts: number;
}

export type EventCallback = (args: EventCallbackPayload) => void;