Notifications
@mavonengine/notifications lets the game server notify players, and anything else you make notifiable, in the game and by mail:
await player.notify(new LevelUp(5))
npx @mavonengine/cli install @mavonengine/notifications
player.notify() and notify() are part of the engine, so the game's code uses them without this plugin in its package.json. Without a notifications plugin they send nothing and say so in the server's log. With this plugin they reach the player.
Players are notified as their accounts, so the project needs an auth plugin such as @mavonengine/auth. For the mail channel it needs @mavonengine/mail as well, and for the database channel a database plugin.
Writing a notification
A notification is a class. via() says which channels it goes to, and a to<Channel>() method what it says on each:
import { Notification } from '@mavonengine/notifications/server'
export class LevelUp extends Notification {
constructor(private level: number) {
super()
}
via() {
return ['game', 'mail']
}
toGame() {
return { level: this.level }
}
toMail() {
return { subject: `Level ${this.level}!`, text: `Well played: you reached level ${this.level}.` }
}
}
via() and every to<Channel>() get the recipient, so a notification can go to some recipients by mail and to others only in the game.
Sending
import { account, notify } from '@mavonengine/core/Notifications/Notifiable'
await player.notify(new LevelUp(5)) // a connected player
await notify(account(accountId), new SeasonEnded()) // an account, connected or not
await notify([guildA, guildB], new WarDeclared()) // several at once
From a plugin, ctx.plugin('@mavonengine/notifications')?.send(to, notification) does the same.
- A player is notified as its account: the notification reaches every game the account has open, not only that one connection. A player without an account, because there is no auth plugin or it was let in without one, is skipped.
- It throws before reaching anyone when the notification names a channel that doesn't exist, or one it has no
to<Channel>()for. - It resolves once every channel has it: sent in the game, handed to the mail plugin, or queued for a queued channel.
Channels
game
Sent over the game socket to every open connection of the recipient's account, right away. A player who isn't connected doesn't get it later; use the database channel for that. In the browser:
import { notifications } from '@mavonengine/notifications/client'
notifications(game).on(n => showToast(n.type, n.data))
notifications(game).onType<{ level: number }>('LevelUp', n => playFanfare(n.data.level))
Each notification also triggers the game event notification: game.on('notification', n => …). It has the id, the type (the class name, or its static notificationType), what toGame() returned as data, and sentAt.
Sent through @mavonengine/mail, which queues it, tries it again and keeps it among the failed jobs when it can't be sent. A notification with queued = false is mailed in the call. toMail() returns a mail written out, like { subject, text, html }.
An account's address comes from @mavonengine/auth. Accounts without an email aren't mailed, and with verifiedEmailOnly only those that confirmed theirs are. Without the mail plugin, or without the auth plugin for accounts, the channel is skipped and the log says so once.
database
Kept in the server's database for the player to read later, also when they weren't connected. Needs a database plugin; without one nothing is kept and the log says so once. The table is notifications_notifications.
export class FriendRequest extends Notification {
constructor(private from: string) {
super()
}
via() {
return ['database', 'game']
}
toDatabase() {
return { from: this.from }
}
toGame() {
return { from: this.from }
}
}
toDatabase() is kept as JSON, up to 64 KB. Each recipient gets a notification of their own, with its own id; notificationId is the same for every recipient and channel of one notify(), and is the id of the game's toast of it.
In the browser, the player reads their account's notifications over the game connection, so it needs no login of its own:
import { notifications } from '@mavonengine/notifications/client'
const inbox = notifications(game)
const { notifications: list, unread } = await inbox.stored({ unread: true, limit: 20 })
await inbox.markRead([list[0].id]) // or a toast's id, or nothing for all of them
inbox.onUnread(count => setBadge(count))
The server tells the game the unread count when it connects and whenever it changes, also when the player read them in another tab: inbox.unread, onUnread() and the game event notifications:unread. A notification is kept before the game is told of it, so marking a toast read always finds it.
On the server, for any recipient:
import { account } from '@mavonengine/core/Notifications/Notifiable'
import { notifications } from '@mavonengine/notifications/server'
const inbox = notifications(server)!
await inbox.stored(account(id), { unread: true, limit: 20, offset: 0 })
await inbox.unreadCount(guild)
await inbox.markRead(account(id), ids) // all of them without ids
await inbox.forget(account(id), ids) // deletes them
Read notifications are deleted keepReadDays after they were read. Unread ones are kept.
Your own
ctx.plugin('@mavonengine/notifications')?.channel('discord', {
queued: true,
route: to => discordWebhookOf(to),
async send(delivery) {
await fetch(delivery.route as string, { method: 'POST', body: JSON.stringify(delivery.payload) })
},
})
A notification goes to it with 'discord' in via() and a toDiscord(). route() says where it goes for a recipient; undefined skips the channel for that recipient. With queued: true, send() runs on the server's job queue and is tried again when it throws.
Making your own classes notifiable
Anything with a stable id can be notified: a guild, a team, a lobby. Extend notifiable() from the engine, which adds notify():
import { notifiable } from '@mavonengine/core/Notifications/Notifiable'
class Guild extends notifiable(GuildRecord) {
readonly notifiableType = 'guild'
notifiableId() {
return this.id
}
// Where each channel goes for the guild: its members' games, its officers' inboxes
routeNotificationFor(channel: string) {
if (channel === 'game')
return this.memberAccountIds
if (channel === 'mail')
return this.officerEmails
}
}
await guild.notify(new WarDeclared(enemy))
notifiable() without a class gives a base class of its own. A class that already extends something you can't change implements Notifiable itself, and its notify() is one line: return notify(this, notification).
routeNotificationFor() is optional and wins over the channel's own route. Without it, game and mail only know accounts. Notifications kept for recipients other than accounts, like a guild, can only be read on the server.
Options
Set them under the plugin in mavonengine.json. All are optional.
| Option | Default | |
|---|---|---|
verifiedEmailOnly | false | Only mail accounts whose address was confirmed with the link the auth plugin mails |
tries | 3 | How often a queued channel's delivery is tried |
backoffMs | [10000, 60000] | How long to wait before trying again, in ms |
keepReadDays | 90 | Delete kept notifications this many days after they were read. 0 keeps them |
With more than one notifications plugin enabled, "notifications": "<package>" in mavonengine.json picks the one notify() uses.
Writing a notifications plugin
A server plugin with a notify() hook is a notifications plugin. It gets the recipients and the notification for every notify() and player.notify():
export default defineServerPlugin({
async notify(to, notification, ctx) {
for (const recipient of to)
await deliver(recipient.notifiableType, recipient.notifiableId(), notification)
},
})
Resolve once it is queued or sent: a rejection is passed on to whoever called notify(). Only the plugin the project picked is asked.