# VDO.Ninja Notifications

Get notified when someone joins your rooms

Setup
Notification History
Settings
Developers
<content>

## Subscribe to Notifications

Notification Topic: This is the value that will be used with ?poke=xxx in your room URL

## How to Use

1. Enter a notification topic above and click Subscribe.
2. Or have a topic automatically generated from your VDO.Ninja room invite link.
3. Add `&poke=YOUR_TOPIC` to your VDO.Ninja room URL.
4. When someone joins that room, you'll receive a notification.
5. Important: Allow Notifications for this site, or keep this page open.

Example Room URL:

For easier setup, use the "Create from VDO.Ninja URL" option below. It will automatically generate a topic based on your room settings and give you a ready-to-use link.

## Create from VDO.Ninja URL

Enter your VDO.Ninja URL to automatically create a notification topic

VDO.Ninja URL:
Generated Topic:
Notification URL for Guests:

## Test Notifications

Click a button below to test the notification system.

## Recent Notifications

No notifications yet.

## Notification Settings

Notification Sound:
Enable Desktop Notifications

## Developer Integration

Learn how to integrate with the VDO.Ninja notification system using Server-Sent Events (SSE).

This API allows developers to subscribe to room join events programmatically using SSE.

### SSE Endpoint

Base URL:

### Connection Example

    		// Basic SSE connection
    		const topic = "your_notification_topic";
    		const evtSource = new EventSource(\`https://notify.vdo.ninja/events?topic=\${topic}\`);

    		// Handle connection
    		evtSource.addEventListener('connect', (event) => {
    		  console.log('Connected to notification service', JSON.parse(event.data));
    		});

    		// Listen for notifications
    		evtSource.addEventListener('notification', (event) => {
    		  const notification = JSON.parse(event.data);
    		  console.log('New notification received:', notification);
    		  // Handle notification - show alert, update UI, etc.
    		});

    		// Handle errors and reconnection
    		evtSource.onerror = (error) => {
    		  console.error('SSE connection error:', error);

    		  // Implement reconnection logic here
    		  if (evtSource.readyState === EventSource.CLOSED) {
    			// Connection closed, try to reconnect
    			setTimeout(() => {
    			  // Reconnect logic
    			}, 2000);
    		  }
    		};

    		// To disconnect
    		function disconnect() {
    		  if (evtSource) {
    			evtSource.close();
    			evtSource = null;
    		  }
    		}

### Fallback Polling

For browsers that don't support SSE or in cases where long-lived connections are problematic:

    		// Polling fallback example
    		async function pollForNotifications(topic, lastPollTime) {
    		  try {
    			const response = await fetch(
    			  \`https://notify.vdo.ninja/poll?topic=\${topic}&since=\${lastPollTime}\`
    			);
    			const data = await response.json();

    			if (data.error) {
    			  console.error('Error during polling:', data.error);
    			} else if (data.notifications && data.notifications.length > 0) {
    			  data.notifications.forEach(notification => {
    				console.log('New notification:', notification);
    				// Handle notification
    			  });
    			}

    			return Date.now(); // Return current time for next poll
    		  } catch (error) {
    			console.error('Error during fallback polling:', error);
    			return lastPollTime; // Keep the same lastPollTime on error
    		  }
    		}

    		// Start polling every minute
    		let lastPollTime = Date.now();
    		const pollingInterval = setInterval(async () => {
    		  lastPollTime = await pollForNotifications('your_topic', lastPollTime);
    		}, 60000);

### Best Practices

- Exponential Backoff: Implement exponential backoff for reconnection attempts to avoid overwhelming the server.
- Fallback Mechanism: Always provide a polling fallback for browsers that don't support SSE.
- Topic Security: Use a secure hash function to generate topics from sensitive room data.
- Error Handling: Properly handle network errors and reconnection scenarios.
- Offline Detection: Detect when the client goes offline and handle reconnection appropriately when back online.
- Testing: Periodically test the connection to ensure it's still alive, as some proxies might silently kill long-lived connections.

### API Reference

Endpoint | Description
--- | ---
`/events?topic={topic}` | SSE endpoint for real-time notifications
`/poll?topic={topic}&since={timestamp}` | Polling endpoint for fetching notifications since a timestamp
`/?notify={topic}&message={message}` | Send a notification to a topic

#### Complete Integration

For a complete integration example, check out the `NotificationManager` class in the source code of this page.

</content>