Real-time Notifications

Build instant notification systems that reach users in real-time.

Overview

NoLag makes it easy to deliver notifications instantly to users across all their devices. This guide covers setting up a notification system with user-specific channels.

Client Setup

import { NoLag } from '@nolag/js-sdk'

const client = NoLag('your_access_token')
await client.connect()

// Subscribe to user-specific notifications
const userRoom = client.setApp('my-app').setRoom(`user-${userId}`)
userRoom.subscribe('notifications')

// Handle incoming notifications
userRoom.on('notifications', (notification) => {
  showNotification({
    title: notification.title,
    body: notification.body,
    icon: notification.icon,
    action: notification.action
  })
})

Sending Notifications (Server)

import { NoLag } from '@nolag/js-sdk'

// Server-side: Connect and send notification to a user
const client = NoLag('your_server_access_token')
await client.connect()

const userRoom = client.setApp('my-app').setRoom('user-123')

userRoom.emit('notifications', {
  title: 'New Message',
  body: 'You have a new message from Alice',
  icon: 'message',
  action: '/messages/456'
})

Notification Types

  • User notifications - Personal notifications for a specific user
  • Broadcast notifications - Announcements to all users
  • Group notifications - Messages to specific user groups

Best Practices

  • Use user-specific rooms for private notifications
  • Include action URLs for clickable notifications
  • Consider notification preferences and quiet hours
  • Use trigger webhooks to also send push notifications for offline users

Next Steps