Notifications
A notification is one message, such as "your invoice was paid", that goes out on one or more channels. Megh has two channels:
| Channel | Delivers through | Needs |
|---|---|---|
mail | Mailer, with your mail settings | app.useMail() |
broadcast | Socket.IO, to a room or to every client | app.useSocketIo() |
Writing a notification
Extend Notification. via() returns the channels; add toMail() and/or toBroadcast() for the channels it returns.
import { Mailable, Notification } from '@meghjs/core';
class InvoicePaidMail extends Mailable {
constructor(private readonly invoice: Invoice) {
super();
}
envelope() {
return { subject: `Invoice ${this.invoice.number} paid` };
}
content() {
return { text: `We received ${this.invoice.total}. Thank you!` };
}
}
export class InvoicePaid extends Notification {
constructor(private readonly invoice: Invoice) {
super();
}
via() {
return ['mail', 'broadcast'] as const;
}
toMail() {
return new InvoicePaidMail(this.invoice);
}
toBroadcast() {
return { data: { invoiceId: this.invoice.id } };
}
}via() gets the recipient, so it can choose channels per user, for example return user.wantsEmail ? ['mail', 'broadcast'] : ['broadcast'].
toBroadcast() returns { data, event?, room? }. event defaults to "notification", and clients receive { type: 'InvoicePaid', data }.
Recipients
Anything with a routeNotificationFor(channel) method can be notified. Return an email address for mail and a Socket.IO room for broadcast:
import type { Notifiable, NotificationChannel } from '@meghjs/core';
export class User implements Notifiable {
id!: number;
email!: string;
routeNotificationFor(channel: NotificationChannel) {
return channel === 'mail' ? this.email : `user.${this.id}`;
}
}With no room (from toBroadcast() or routeNotificationFor), a broadcast goes to every connected client.
Sending
Inject Notifier, or use the notifier helper:
import { Notifier } from '@meghjs/core';
@AppService
export class BillingService {
constructor(private readonly notifier: Notifier) {}
async markPaid(invoice: Invoice, user: User) {
// ...
await this.notifier.send(user, new InvoicePaid(invoice));
}
}send() also takes an array, and sends to each recipient in turn.
Sending throws NotificationError when a channel in via() has no matching to*() method, when routeNotificationFor('mail') returns no address, or when broadcast is used without app.useSocketIo().
Send in the background
Notifications are sent while you wait. To keep a request fast, return a mailable that is sent through the mail queue, or dispatch the notification from a queue job.
Testing
Notifier.fake() records notifications instead of sending them:
import { Notifier } from '@meghjs/core';
afterEach(() => Notifier.restore());
it('notifies the customer', async () => {
const notifications = Notifier.fake();
await billing.markPaid(invoice, user);
notifications.assertSentTo(user, InvoicePaid);
expect(notifications.sentTo(user, InvoicePaid)).toHaveLength(2); // mail + broadcast
});| Method | Checks |
|---|---|
assertSentTo(user, Type) | At least one Type was sent to user |
assertNothingSent() | Nothing was sent |
sentTo(user, Type) | Returns the recorded sends, one per channel |
sent | Every recorded { notifiable, notification, channel } |