Appearance
Widget Installation
The Nexus notification widget allows you to add real-time in-app notifications to your application with just a few lines of code.
Features
- 🔔 Floating notification bell
- 📱 Responsive design
- 🎨 Customizable themes
- 🔄 Real-time updates
- 📋 Channel filtering
- 🔐 Secure authentication
Installation
Method 1: Script Tag (Recommended)
The simplest way to add the Nexus widget is using a script tag:
html
<!-- Add before closing </body> tag -->
<script src="https://api.nexus.dev/widget/nexus-widget.js"></script>
<script>
NexusWidget.init({
environmentId: 'your-environment-id',
subscriberHash: subscriberHash, // signed by your backend (see below)
subscriberId: 'current-user-id',
position: 'bottom-right'
});
</script>Sign the subscriber on your server
The widget runs in your users' browsers, so it must never receive your API key, which grants full access to your account. Your backend instead signs the current user's ID with your environment's widget secret, and the widget sends that signature (subscriberHash) to prove who it is acting for. A visitor can only ever see their own notifications.
The widget secret is returned as widgetSecret for each environment by GET /api/auth/me. Keep it server-side, like any other secret.
javascript
import { createHmac } from 'crypto'
// subscriberId must match the externalId you use when creating subscribers / triggering
const subscriberHash = createHmac('sha256', process.env.NEXUS_WIDGET_SECRET)
.update(currentUser.id)
.digest('hex')
// Render subscriberHash into the page, or return it from an authenticated endpointpython
import hmac, hashlib, os
subscriber_hash = hmac.new(
os.environ["NEXUS_WIDGET_SECRET"].encode(),
current_user.id.encode(),
hashlib.sha256,
).hexdigest()Method 2: NPM Package
For modern JavaScript applications:
bash
npm install @nexus/widgetbash
yarn add @nexus/widgetbash
bun add @nexus/widgetbash
pnpm add @nexus/widgetThen initialize in your application:
javascript
import { NexusWidget } from '@nexus/widget'
// Initialize the widget
const widget = NexusWidget.init({
environmentId: 'your-environment-id',
subscriberHash: subscriberHash, // signed by your backend (see below)
subscriberId: 'current-user-id',
position: 'bottom-right',
theme: 'light'
})
// Listen for events
widget.on('notification.clicked', (notification) => {
console.log('Notification clicked:', notification)
})
// Programmatically add notifications
widget.addNotification({
title: 'New Message',
message: 'You have a new message from John',
channel: 'social'
})typescript
import { NexusWidget, WidgetOptions, Notification } from '@nexus/widget'
// Define configuration
const options: WidgetOptions = {
environmentId: 'your-environment-id',
subscriberHash: subscriberHash, // signed by your backend (see below)
subscriberId: 'current-user-id',
position: 'bottom-right',
theme: 'light'
}
// Initialize the widget
const widget = NexusWidget.init(options)
// Listen for events with types
widget.on('notification.clicked', (notification: Notification) => {
console.log('Notification clicked:', notification)
})
// Programmatically add notifications
widget.addNotification({
title: 'New Message',
message: 'You have a new message from John',
channel: 'social'
})vue
<template>
<div id="app">
<!-- Your app content -->
</div>
</template>
<script setup>
import { onMounted, onUnmounted } from 'vue'
import { NexusWidget } from '@nexus/widget'
let widget
onMounted(() => {
// Initialize widget
widget = NexusWidget.init({
environmentId: 'your-environment-id',
subscriberHash: subscriberHash, // signed by your backend (see below)
subscriberId: getCurrentUserId(),
position: 'bottom-right'
})
// Listen for events
widget.on('notification.clicked', handleNotificationClick)
})
onUnmounted(() => {
// Clean up
if (widget) {
widget.destroy()
}
})
function handleNotificationClick(notification) {
console.log('Notification clicked:', notification)
// Handle navigation or actions
}
function getCurrentUserId() {
// Return current user ID from your auth system
return 'user-123'
}
</script>jsx
import { useEffect, useRef } from 'react'
import { NexusWidget } from '@nexus/widget'
function App() {
const widgetRef = useRef(null)
useEffect(() => {
// Initialize widget
widgetRef.current = NexusWidget.init({
environmentId: 'your-environment-id',
subscriberHash: subscriberHash, // signed by your backend (see below)
subscriberId: getCurrentUserId(),
position: 'bottom-right'
})
// Listen for events
const handleClick = (notification) => {
console.log('Notification clicked:', notification)
}
widgetRef.current.on('notification.clicked', handleClick)
// Cleanup
return () => {
if (widgetRef.current) {
widgetRef.current.destroy()
}
}
}, [])
return (
<div className="App">
{/* Your app content */}
</div>
)
}
function getCurrentUserId() {
// Return current user ID from your auth system
return 'user-123'
}
export default Appsvelte
<script>
import { onMount, onDestroy } from 'svelte'
import { NexusWidget } from '@nexus/widget'
let widget
onMount(() => {
// Initialize widget
widget = NexusWidget.init({
environmentId: 'your-environment-id',
subscriberHash: subscriberHash, // signed by your backend (see below)
subscriberId: getCurrentUserId(),
position: 'bottom-right'
})
// Listen for events
widget.on('notification.clicked', (notification) => {
console.log('Notification clicked:', notification)
})
})
onDestroy(() => {
// Clean up
if (widget) {
widget.destroy()
}
})
function getCurrentUserId() {
// Return current user ID from your auth system
return 'user-123'
}
</script>
<main>
<!-- Your app content -->
</main>Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
environmentId | string | required | Your Nexus environment ID (not secret) |
subscriberId | string | required | Current user's ID (the subscriber's externalId) |
subscriberHash | string | required | HMAC-SHA256 of subscriberId with your widget secret, hex-encoded, computed on your server |
apiUrl | string | 'http://localhost:3001' | Nexus API base URL |
position | string | 'bottom-right' | Widget position: bottom-right, bottom-left, top-right, top-left |
theme | string | 'light' | Theme: light or dark |
channels | array | ['all'] | Enabled channels |
refreshInterval | number | 30000 | Auto-refresh interval in ms |
maxNotifications | number | 50 | Maximum notifications to display |
sound | boolean | false | Enable notification sounds |
browserNotifications | boolean | true | Request browser notification permission |
Styling
The widget automatically adapts to your application's theme. You can customize it further:
css
/* Override widget styles */
.nexus-widget-container {
--nexus-primary: #667eea;
--nexus-primary-dark: #764ba2;
--nexus-border-radius: 12px;
--nexus-font-family: 'Inter', sans-serif;
}
/* Custom notification bell color */
.nexus-widget-button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
}
/* Custom badge color */
.nexus-widget-badge {
background: #ef4444 !important;
}Events
Listen to widget events to integrate with your application:
javascript
// Notification clicked
widget.on('notification.clicked', (notification) => {
// Handle notification click
})
// Notification marked as read
widget.on('notification.read', (notificationId) => {
// Update your UI
})
// Widget opened
widget.on('widget.opened', () => {
// Track analytics
})
// Widget closed
widget.on('widget.closed', () => {
// Save state
})
// Settings clicked
widget.on('settings.clicked', () => {
// Open settings page
})API Methods
addNotification(notification)
Programmatically add a notification:
javascript
widget.addNotification({
title: 'Order Shipped',
message: 'Your order #12345 has been shipped',
channel: 'system',
data: { orderId: '12345' }
})markAsRead(notificationId)
Mark a notification as read:
javascript
widget.markAsRead('notification-123')clearAll()
Clear all notifications:
javascript
widget.clearAll()destroy()
Remove the widget:
javascript
widget.destroy()Security
The widget never uses your API key:
- Environment ID: Identifies your environment. It's public and safe to embed.
- Subscriber ID + hash: Your backend signs the user's ID with your widget secret (HMAC-SHA256). The API only returns notifications for a subscriber whose hash verifies, so users can't read each other's notifications by changing the ID.
- No cookies: Widget requests are sent without credentials, so they can't be used to act on behalf of a logged-in dashboard user.
DANGER
If you ever passed your API key (sk_…) to the widget, assume it is public: rotate it immediately.
Troubleshooting
Widget not appearing
- Check browser console for errors
- Verify
environmentId,subscriberIdandsubscriberHashare all provided - A
401means the hash doesn't match: check it's computed from the exactsubscriberIdwith the right environment's widget secret - Check CORS settings in Nexus dashboard
Notifications not updating
- Verify WebSocket connection
- Check network tab for API errors
- Ensure user has proper permissions
Style conflicts
- Use more specific CSS selectors
- Check for CSS-in-JS conflicts
- Use the
!importantflag sparingly