Skip to main content

Push Notifications

This service provides helpers to subscribe and unsubscribe to the Pushex push notification server.

Examples

Subscribing to an Event

We can subscribe to a channel/event pair using subscribe. A function is returned that can remove the subscription. Additionally, a generic can be provided to type the payload the subscription callback will receive.

import { subscribe } from '@rhapsody/push-notifications'

subscribe<{ id: number }>('accounts', 'account.updated', (payload) => {
console.log(`Account ID ${payload.id} updated.`)
})

Unsubscribing to an Event

We can remove all subscriptions for a given channel/event pair using unsubscribe.

import { unsubscribe } from '@rhapsody/push-notifications'

unsubscribe('accounts', 'account.updated')

Get the Underlying Pushex Client

Advanced

We can access the underlying Pushex client (or instantiate it if it doesn't exist) using getClient.

import { getClient } from '@rhapsody/push-notifications'

const subscription = getClient().getExistingSubscription('accounts')
// Do something with the subscription here

Disconnect from the Push Notification Server

Advanced

We can disconnect the client from the server using disconnect.

import { disconnect } from '@rhapsody/push-notifications'

if (window.disablePushNotifications) {
disconnect()
}