A comprehensive booking management system built with Next.js, React, and Socket.IO, providing real-time updates and push notifications.
GitHub →This project is a comprehensive booking management system built with Next.js, React, and Socket.IO. It provides functionality for users to view, create, edit, and cancel bookings, with real-time updates across all connected clients.
BookingsPage: Displays a list of user bookings with sorting options.BookingDetailPage: Shows detailed information for a single booking, with options to edit and cancel.ServicesPage: Displays a list of available services with search and filter functionality.ServiceDetailPage: Shows detailed information for a single service, allowing users to book the service.NavBar: Navigation component for the application.Footer: Footer component for the application.Loading: Loading indicator component./api/auth/[...nextauth]: Handles authentication using NextAuth.js./api/auth/signup: Handles user registration./api/bookings: Handles fetching and creating bookings./api/bookings/[id]: Handles fetching, updating, and cancelling individual bookings./api/services: Handles fetching and creating services.Our project implements a real-time push notification system to keep users informed about style updates across the application. This system ensures that all connected clients receive instant updates when global styles are changed.
Server-Side Setup
The server uses the web-push library to handle push notifications:
const webpush = require('web-push');
const vapidDetails = {
subject: 'mailto:your-email@example.com',
publicKey: 'YOUR_PUBLIC_VAPID_KEY',
privateKey: 'YOUR_PRIVATE_VAPID_KEY'
};
webpush.setVapidDetails(
vapidDetails.subject,
vapidDetails.publicKey,
vapidDetails.privateKey
);Client-Side Subscription When a user visits the site, their browser subscribes to push notifications:
if ('serviceWorker' in navigator && 'PushManager' in window) {
const registration = await navigator.serviceWorker.register('/service-worker.js');
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: vapidPublicKey
});
await fetch('/api/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(subscription)
});
}Sending Notifications When styles are updated, the server sends notifications to all subscribed clients:
const subscriptions = await db.collection('pushSubscriptions').find({}).toArray();
for (const subscription of subscriptions) {
try {
await webpush.sendNotification(subscription, JSON.stringify({
title: 'Style Update',
body: 'The website style has been updated.',
data: updatedStyles
}));
} catch (error) {
console.error('Error sending push notification:', error);
}
}Service Worker
The service worker (public/service-worker.js) handles incoming push events:
self.addEventListener('push', function(event) {
const data = event.data.json();
self.registration.showNotification(data.title, {
body: data.body,
data: data.data
});
});Client-Side Handling The main application listens for messages from the service worker:
navigator.serviceWorker.addEventListener('message', (event) => {
if (event.data && event.data.type === 'STYLE_UPDATE') {
updateStyles(event.data.styles);
}
});This system enables instant style updates across all connected clients without requiring a page refresh, enhancing the user experience with real-time synchronization.
npm installnpm run devCreate a .env.local file in the root directory with the following variables:
These environment variables are used as follows:
MONGODB_URI: This is your MongoDB connection string. It's used to connect to your database.NEXTAUTH_SECRET: This is a secret key used by NextAuth.js for encryption. Make sure to keep this secret and don't share it publicly.To use these environment variables in your code, replace hardcoded values with process.env.VARIABLE_NAME. For example:
const mongoUrl = process.env.MONGODB_URI || '';
The MongoDB connection string and NextAuth secret in this example are placeholders. In a real-world application, you should:
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.