-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
168 lines (145 loc) · 4.46 KB
/
Copy pathserver.js
File metadata and controls
168 lines (145 loc) · 4.46 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { createServer } from "http";
import next from "next";
import { Server } from "socket.io";
import cookie from "cookie";
import jwt from "jsonwebtoken";
import axios from "axios";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const ORIGIN = process.env.ALLOWED_ORIGIN || "http://localhost:3000";
const PUBLIC_BASE_URL = process.env.PUBLIC_BASE_URL || "http://localhost:3000";
const WS_JWT_SECRET =
process.env.WS_JWT_SECRET || process.env.JWT_SECRET || "change-me-in-prod";
// Helper: Verify JWT safely
function verifyToken(token) {
if (!token) return null;
try {
return jwt.verify(token, WS_JWT_SECRET);
} catch {
return null;
}
}
app.prepare().then(() => {
const server = createServer((req, res) => handle(req, res));
const io = new Server(server, {
cors: {
origin: ORIGIN === "*" ? true : ORIGIN,
credentials: true,
},
transports: ["websocket", "polling"],
});
/**
* Widget namespace (site visitors)
*/
const widgetNS = io.of("/widget");
widgetNS.use((socket, next) => {
const token = socket.handshake.auth?.token;
const payload = verifyToken(token);
if (!payload || payload.kind !== "widget") {
return next(new Error("Unauthorized: Invalid widget token"));
}
socket.data.companyId = payload.companyId;
socket.data.conversationId = payload.conversationId;
next();
});
/**
* Agent namespace (dashboard)
*/
const agentNS = io.of("/agent");
agentNS.use((socket, next) => {
const rawCookie = socket.handshake.headers?.cookie || "";
const cookies = cookie.parse(rawCookie);
const token = socket.handshake.auth?.token || cookies.vc_session || null;
const payload = verifyToken(token);
if (!payload || payload.kind !== "agent") {
return next(new Error("Unauthorized: Invalid agent token"));
}
socket.data.agentId = payload.sub;
socket.data.companyId = payload.companyId;
next();
});
/**
* Widget connection handlers
*/
widgetNS.on("connection", (socket) => {
const room = `c_${socket.data.conversationId}`;
socket.join(room);
socket.on("message:send", async (msg) => {
try {
const messageBody = String(msg?.body || "").trim();
if (!messageBody) {
return socket.emit("message:error", "Message body is required");
}
await axios.post(
`${PUBLIC_BASE_URL}/api/messages`,
{
conversationId: socket.data.conversationId,
body: messageBody,
sender: "visitor",
},
{
headers: { "Content-Type": "application/json" },
}
);
agentNS.to(room).emit("message:new", {
body: messageBody,
sender: "visitor",
conversationId: socket.data.conversationId,
});
socket.emit("message:ack");
} catch (err) {
console.error("Widget message error:", err.message);
socket.emit("message:error", "Failed to save message");
}
});
socket.on("disconnect", () => {
console.log(`Widget disconnected from ${room}`);
});
});
/**
* Agent connection handlers
*/
agentNS.on("connection", (socket) => {
socket.on("conversation:join", ({ conversationId }) => {
if (!conversationId) return;
socket.join(`c_${conversationId}`);
});
socket.on("message:send", async (payload) => {
try {
const { conversationId, body } = payload || {};
if (!conversationId || !body) {
return socket.emit("message:error", "Invalid message payload");
}
const messageBody = String(body).trim();
await axios.post(
`${PUBLIC_BASE_URL}/api/messages`,
{
conversationId,
body: messageBody,
sender: "agent",
},
{
headers: { "Content-Type": "application/json" },
}
);
widgetNS.to(`c_${conversationId}`).emit("message:new", {
body: messageBody,
sender: "agent",
conversationId,
});
socket.emit("message:ack");
} catch (err) {
console.error("Agent message error:", err.message);
socket.emit("message:error", "Failed to send message");
}
});
});
/**
* Start server
*/
const port = parseInt(process.env.PORT || "3000", 10);
server.listen(port, () => {
console.log(`✅ Server ready on http://localhost:${port}`);
});
});