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)
// Server-side: Send notification to a user
await nolag.publish({
appId: 'app_xxx',
topic: 'user-123/notifications',
data: {
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