-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
31 lines (28 loc) · 895 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const { createServer } = require("http");
const { Server } = require("socket.io");
const next = require("next");
const hostname = process.env.HOST || "localhost";
const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();
app.prepare().then(() => {
const httpServer = createServer(handler);
const io = new Server(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
socket.on("notify", (notificationObject) => {
io.emit("notify", notificationObject);
});
});
httpServer.once("error",(err)=>{
console.error(err);
process.exit(1);
}).listen(port, () => {
console.log("Server running on port 4000");
});
})