A JavaScript library for integrating real-time notifications from the Hermes notification system into web applications using Server-Sent Events (SSE).
- 🔄 Real-time notifications via Server-Sent Events (SSE)
- ⚛️ React hooks for easy integration
- 🎨 Pre-built components with customizable styling
- 🔐 Token-based authentication
- 🔄 Automatic reconnection with exponential backoff
- 📱 Responsive design with mobile support
- 🌙 Dark mode support
- 📊 Connection status monitoring
- 🎯 TypeScript support
npm install hermes-notifierOr with Yarn:
yarn add hermes-notifierimport HermesNotifier from 'hermes-notifier';
const notifier = new HermesNotifier({
baseUrl: 'http://localhost:8000',
token: 'your-api-token',
userId: 'user-123',
});
// Subscribe to notifications
notifier.onNotification((notification) => {
console.log('New notification:', notification);
// Display notification to user
});
// Connect to the notification stream
await notifier.connect();import React, { useEffect } from 'react';
import { useHermesNotifications } from 'hermes-notifier/react-hooks';
import 'hermes-notifier/styles.css';
function App() {
const {
notifications,
connectionStatus,
unreadCount,
connect,
disconnect,
markAsRead,
} = useHermesNotifications({
baseUrl: 'http://localhost:8000',
token: 'your-api-token',
userId: 'user-123',
});
useEffect(() => {
connect();
return () => disconnect();
}, []);
return (
<div>
<h1>My App</h1>
<div>
Connection: {connectionStatus.connected ? 'Connected' : 'Disconnected'}
| Unread: {unreadCount}
</div>
<div>
{notifications.map((notification) => (
<div key={notification.id}>
<h3>{notification.title}</h3>
<p>{notification.body}</p>
{!notification.is_read && (
<button onClick={() => markAsRead(notification.id)}>
Mark as Read
</button>
)}
</div>
))}
</div>
</div>
);
}import React, { useEffect } from 'react';
import { useHermesNotifications } from 'hermes-notifier/react-hooks';
import { NotificationList, ConnectionStatus } from 'hermes-notifier/react-components';
import 'hermes-notifier/styles.css';
function NotificationCenter() {
const {
notifications,
connectionStatus,
connect,
disconnect,
markAsRead,
} = useHermesNotifications({
baseUrl: 'http://localhost:8000',
token: 'your-api-token',
userId: 'user-123',
});
useEffect(() => {
connect();
return () => disconnect();
}, []);
return (
<div>
<ConnectionStatus {...connectionStatus} />
<NotificationList
notifications={notifications}
onMarkAsRead={markAsRead}
maxItems={10}
/>
</div>
);
}new HermesNotifier(config: HermesConfig)HermesConfig:
baseUrl(string): Base URL of the Hermes APItoken(string): Authentication tokenuserId(string): User ID to receive notifications forreconnectDelay(number, optional): Delay between reconnection attempts (default: 5000ms)maxReconnectAttempts(number, optional): Maximum reconnection attempts (default: 10)
connect(): Connect to the notification streamdisconnect(): Disconnect from the streamisConnected(): Check connection statusonNotification(handler): Subscribe to notification eventsonConnectionChange(handler): Subscribe to connection status changesonError(handler): Subscribe to error eventsmarkAsRead(notificationId): Mark a notification as readgetUnreadCount(): Get unread notification countsendTestNotification(title, body): Send a test notification
Main hook for managing notifications in React components.
const {
notifications,
connectionStatus,
unreadCount,
connect,
disconnect,
markAsRead,
clearNotifications,
sendTestNotification,
isConnected,
} = useHermesNotifications(config);Simplified hook that only tracks the latest notification.
const {
latestNotification,
connectionStatus,
connect,
disconnect,
clearLatest,
isConnected,
} = useLatestNotification(config);Display a single notification.
<NotificationItem
notification={notification}
onMarkAsRead={handleMarkAsRead}
onClose={handleClose}
autoClose={true}
autoCloseDelay={5000}
/>Display a list of notifications.
<NotificationList
notifications={notifications}
onMarkAsRead={handleMarkAsRead}
onClose={handleClose}
maxItems={10}
/>Display notifications as toast messages.
<NotificationToast
notification={notification}
position="top-right"
autoClose={true}
onClose={handleClose}
/>Display connection status indicator.
<ConnectionStatus
connected={connectionStatus.connected}
reconnecting={connectionStatus.reconnecting}
error={connectionStatus.error}
/>The plugin uses token-based authentication. You need to obtain a token from the Hermes API:
// Get a token from your Hermes backend
const response = await fetch('http://localhost:8000/api/v1/tokens/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'My App Token',
permissions: {
can_send_notifications: true,
can_read_notifications: true,
},
expires_in_days: 30,
}),
});
const { token } = await response.json();The plugin includes default CSS styles that you can import:
import 'hermes-notifier/styles.css';You can override the default styles by targeting the CSS classes:
.hermes-notification {
/* Custom notification styling */
}
.hermes-notification-critical {
/* Custom critical priority styling */
}
.hermes-toast {
/* Custom toast styling */
}const config = {
baseUrl: 'http://localhost:8000',
token: 'your-token-here',
userId: 'user-123',
};const config = {
baseUrl: 'https://notifications.yourapp.com',
token: process.env.REACT_APP_HERMES_TOKEN,
userId: getCurrentUserId(),
reconnectDelay: 3000,
maxReconnectAttempts: 15,
};const notifier = new HermesNotifier(config);
notifier.onError((error) => {
console.error('Notification error:', error);
// Handle error (show user message, retry, etc.)
});
notifier.onConnectionChange((status) => {
if (status.error) {
console.error('Connection error:', status.error);
}
});- Chrome 60+
- Firefox 55+
- Safari 11+
- Edge 79+
Server-Sent Events are supported in all modern browsers. For older browsers, consider using a polyfill.
To build the plugin locally:
npm install
npm run buildTo run tests:
npm testMIT License - see LICENSE file for details.
For issues and questions, please visit the GitHub repository.