Skip to main content

Class: NotificationUtils

Defined in: core/src/NotificationsUtils.ts:12

Utility functions for sending push notifications to registered clients.

Constructors

Constructor

new NotificationUtils(redis): NotificationUtils

Defined in: core/src/NotificationsUtils.ts:40

Initializes the utility using the given redis connection.

Parameters

redis

any

The redis connection to publish to.

Returns

NotificationUtils

Properties

BROADCAST_CHANNEL

readonly static BROADCAST_CHANNEL: "broadcast:allusers" = "broadcast:allusers"

Defined in: core/src/NotificationsUtils.ts:20

The redis channel broadcastMessage() publishes to. Namespaced (rather than a bare "allusers") so it can never collide with a per-recipient channel sendMessage() derives from a caller-supplied uid - a client-facing "send a message to this uid" feature that passes its recipient straight through to sendMessage() must not be able to choose a uid that lands on the broadcast channel and reach every subscriber instead of the intended one recipient.


USER_CHANNEL_PREFIX

readonly static USER_CHANNEL_PREFIX: "user:" = "user:"

Defined in: core/src/NotificationsUtils.ts:26

Prefix applied to every per-recipient channel in sendMessage(). Combined with BROADCAST_CHANNEL's own distinct "broadcast:" prefix, this guarantees the two channel namespaces can never overlap regardless of what uid value a caller supplies.

Methods

broadcastMessage()

broadcastMessage(type, action, data): void

Defined in: core/src/NotificationsUtils.ts:76

Broadcasts a given message to all users.

Subscribers must listen on NotificationUtils.BROADCAST_CHANNEL (rather than a bare "allusers") to receive these messages.

Parameters

type

any

The type of message being sent.

action

string

The action performed on the data (if applicable).

data

any

The contents of the message to send.

Returns

void


sendMessage()

sendMessage(uids, type, action, data): void

Defined in: core/src/NotificationsUtils.ts:93

Sends a given message to the room or user with the specified uid(s).

Each recipient's channel is NotificationUtils.USER_CHANNEL_PREFIX + uid (rather than the bare uid), so that no caller-supplied uid can collide with NotificationUtils.BROADCAST_CHANNEL or any other reserved channel and be delivered to unintended subscribers. Subscribers must listen on that prefixed channel name to receive messages sent to their uid.

Parameters

uids

string | string[]

The universally unique identifier of the room or user to send the message to.

type

string

The type of message being sent.

action

string

The action performed on the data (if applicable).

data

any

The contents of the message to send to the room or user.

Returns

void