NODE JS introduction: socket polling
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
# Node.js real-time server config
|
||||
# Copy this file to .env and fill in the values
|
||||
|
||||
# Port the Node.js server listens on
|
||||
PORT=3000
|
||||
|
||||
# Secret key — PHP sends this in x-emit-secret header to authenticate /emit calls
|
||||
# Use a long random string in production
|
||||
EMIT_SECRET=
|
||||
Generated
+1099
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "mn3wms-realtime",
|
||||
"version": "1.0.0",
|
||||
"description": "Real-time event server for MN3 WMS",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"start": "node server.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.0",
|
||||
"socket.io": "^4.7.0",
|
||||
"dotenv": "^16.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
require('dotenv').config();
|
||||
|
||||
const express = require('express');
|
||||
const { createServer } = require('http');
|
||||
const { Server } = require('socket.io');
|
||||
|
||||
const app = express();
|
||||
const httpServer = createServer(app);
|
||||
const io = new Server(httpServer, {
|
||||
cors: { origin: '*' }
|
||||
});
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
// ── /emit ─────────────────────────────────────────────────────────────────────
|
||||
// PHP calls this after any significant action.
|
||||
// Body: { event, data, company_id }
|
||||
//
|
||||
app.post('/emit', (req, res) => {
|
||||
const secret = req.headers['x-emit-secret'];
|
||||
if (secret !== process.env.EMIT_SECRET) {
|
||||
return res.status(403).json({ ok: false, message: 'Forbidden' });
|
||||
}
|
||||
|
||||
const { event, data, company_id } = req.body;
|
||||
if (!event || !company_id) {
|
||||
return res.status(400).json({ ok: false, message: 'event and company_id required' });
|
||||
}
|
||||
|
||||
// Broadcast only to the room for this company
|
||||
io.to(`company_${company_id}`).emit(event, data);
|
||||
|
||||
console.log(`[emit] company=${company_id} event=${event}`, data);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// ── WebSocket connections ─────────────────────────────────────────────────────
|
||||
// Each browser tab connects here on page load.
|
||||
// It joins a room named company_<id> so events stay isolated per company.
|
||||
//
|
||||
io.on('connection', (socket) => {
|
||||
const company_id = socket.handshake.query.company_id;
|
||||
|
||||
if (!company_id) {
|
||||
socket.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
socket.join(`company_${company_id}`);
|
||||
console.log(`[connect] socket=${socket.id} company=${company_id}`);
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log(`[disconnect] socket=${socket.id}`);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Start ─────────────────────────────────────────────────────────────────────
|
||||
const PORT = process.env.PORT || 3000;
|
||||
httpServer.listen(PORT, () => {
|
||||
console.log(`Node.js real-time server running on port ${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user