-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
49 lines (41 loc) · 1.26 KB
/
server.ts
File metadata and controls
49 lines (41 loc) · 1.26 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { createServer } from "http";
// import { parse } from "url";
import next from "next";
import { Server } from "socket.io";
import express from "express";
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = 3000;
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
const httpServer = createServer(server);
const io = new Server(httpServer, {
path: "/api/socket/io",
addTrailingSlash: false,
});
io.on("connection", (socket) => {
console.log("Client connected", socket.id);
socket.on("join-room", (room) => {
socket.join(room);
console.log(`User joined room: ${room}`);
// Test Notification
socket.emit("notification:new", {
type: "success",
title: "Welcome to CookHub!",
message: "Real-time notifications are now active.",
});
});
socket.on("disconnect", () => {
console.log("Client disconnected");
});
});
server.all(/(.*)/, (req, res) => {
return handle(req, res);
});
httpServer.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
console.log(`> Socket.io server running at path: /api/socket/io`);
});
});