diff options
author | 2025-04-29 10:47:43 +0530 | |
---|---|---|
committer | 2025-04-29 10:47:43 +0530 | |
commit | a2e0a65b2599267efe94d665d6305f59b225bbd5 (patch) | |
tree | e2cef2031e3f7655e0c5f419020a3f1064c3b7b8 /backend/src/socket | |
parent | 570bf0f3f065d583d6f94ecfc61aae93ba3e43de (diff) | |
download | restaurant-a2e0a65b2599267efe94d665d6305f59b225bbd5.tar.gz restaurant-a2e0a65b2599267efe94d665d6305f59b225bbd5.tar.bz2 restaurant-a2e0a65b2599267efe94d665d6305f59b225bbd5.zip |
Diffstat (limited to 'backend/src/socket')
-rw-r--r-- | backend/src/socket/handlers.js | 51 | ||||
-rw-r--r-- | backend/src/socket/index.js | 42 |
2 files changed, 93 insertions, 0 deletions
diff --git a/backend/src/socket/handlers.js b/backend/src/socket/handlers.js new file mode 100644 index 0000000..7e110b5 --- /dev/null +++ b/backend/src/socket/handlers.js @@ -0,0 +1,51 @@ +/** + * Register all event handlers for a socket connection + * @param {object} io - Socket.IO server instance + * @param {object} socket - Socket connection instance + */ +const registerHandlers = (io, socket) => { + // Handle joining a room (e.g., for orders or staff notifications) + socket.on('join-room', (room) => { + socket.join(room); + console.log(`Client ${socket.id} joined room: ${room}`); + }); + + // Handle order updates + socket.on('order-update', (data) => { + // Broadcast order updates to all clients in the room + io.to('staff-notifications').emit('order-status-changed', data); + io.to(`user-${data.userId}`).emit('order-status-changed', data); + console.log(`Order update: ${JSON.stringify(data)}`); + }); + + // Handle table reservation updates + socket.on('reservation-update', (data) => { + // Broadcast reservation updates to staff + io.to('staff-notifications').emit('reservation-changed', data); + console.log(`Reservation update: ${JSON.stringify(data)}`); + }); + + // Handle staff notifications + socket.on('staff-notification', (data) => { + // Broadcast to specific staff members or all staff + io.to('staff-notifications').emit('new-notification', data); + console.log(`Staff notification: ${JSON.stringify(data)}`); + }); + + // Handle customer notifications + socket.on('customer-notification', (data) => { + // Send notification to specific customer + io.to(`user-${data.userId}`).emit('new-notification', data); + console.log(`Customer notification: ${JSON.stringify(data)}`); + }); + + // Leave room + socket.on('leave-room', (room) => { + socket.leave(room); + console.log(`Client ${socket.id} left room: ${room}`); + }); +}; + +module.exports = { + registerHandlers +};
\ No newline at end of file diff --git a/backend/src/socket/index.js b/backend/src/socket/index.js new file mode 100644 index 0000000..0c25ed4 --- /dev/null +++ b/backend/src/socket/index.js @@ -0,0 +1,42 @@ +const socketIO = require('socket.io'); +const handlers = require('./handlers'); + +/** + * Initialize Socket.IO + * @param {object} server - HTTP server instance + * @returns {object} Socket.IO instance + */ +const initializeSocket = (server) => { + const io = socketIO(server, { + cors: { + origin: '*', + methods: ['GET', 'POST'] + } + }); + + // Authentication middleware for socket connections + io.use((socket, next) => { + // Simple authentication can be added here if needed + // const token = socket.handshake.auth.token; + // if (token validation logic) next(); + // else next(new Error('Authentication error')); + next(); + }); + + // Handle connection + io.on('connection', (socket) => { + console.log(`New client connected: ${socket.id}`); + + // Register event handlers + handlers.registerHandlers(io, socket); + + // Handle disconnection + socket.on('disconnect', () => { + console.log(`Client disconnected: ${socket.id}`); + }); + }); + + return io; +}; + +module.exports = initializeSocket;
\ No newline at end of file |