Skip to main content

Notifications

Where events are for services talking to each other, NotificationUtils is for pushing a message to one or more specific, currently-connected users: the kind of thing a WebSocket route forwards straight to a client.

class NotificationUtils {
constructor(redis: Redis);
sendMessage(uids: string | string[], type: string, action: string, data: any): void;
}

sendMessage publishes a JSON payload ({type, action, data}) to a Redis channel named after each recipient's uid. On the receiving end, BasePushRoute is the concrete implementation of that idea: a WebSocket route that subscribes each connected client to its own uid channel (plus any others it has permission for) and forwards whatever's published there straight to the socket. A CLI-scaffolded project already has this wired up as PushRoute at /push; see Base Routes: Push for how connect and subscriptions work in detail.

Because its constructor takes a Redis connection directly rather than using property injection, construct it explicitly through the ObjectFactory with the connection you want it to publish through:

import {ObjectDecorators} from '@rapidrest/core';
import {NotificationUtils, DatabaseDecorators} from '@rapidrest/service-core';
import type {Redis} from 'ioredis';
const {Inject} = ObjectDecorators;
const {RedisConnection} = DatabaseDecorators;

export class PetService {
@RedisConnection('cache')
private redis?: Redis;

@Inject(ObjectFactory)
private objectFactory?: ObjectFactory;

async notifyOwner(ownerUid: string, pet: Pet) {
const notifications = await this.objectFactory?.newInstance(NotificationUtils, {args: [this.redis]});
notifications?.sendMessage(ownerUid, 'pet', 'updated', pet);
}
}

Two more utilities from @rapidrest/core round out the notification story:

  • Messaging - MessagingUtils, for templated email, Slack, and SMS/Twilio messaging.
  • Alerts - AlertUtils, for structured, priority-leveled incident alerts (PagerDuty/Opsgenie-style), aimed at your team rather than your users.