Skip to content

Commit 5c3feb3

Browse files
author
Ahad-webdev-90
committed
Chatify
1 parent 3493ee9 commit 5c3feb3

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

server/socket/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ const app = express();
202202
const server = http.createServer(app);
203203
const io = new Server(server, {
204204
cors: {
205-
origin: "https://chatify-ahkg.vercel.app", // Ensure this matches your deployment
205+
origin: ["https://chatify-ahkg.vercel.app", "https://chatify-two-drab.vercel.app"], // Added multiple origins
206206
methods: ["GET", "POST"],
207207
allowedHeaders: ["Authorization"],
208208
credentials: true
209209
},
210-
transports: ['websocket', 'polling'] // Enable both websocket and polling transports
210+
transports: ['websocket', 'polling'] // Enable both websocket and polling transports, prioritizing websocket
211211
});
212212

213213
// Track online users
@@ -216,7 +216,7 @@ const onlineUser = new Set();
216216
io.on('connection', async (socket) => {
217217
console.log("User connected:", socket.id);
218218

219-
const token = socket.handshake.auth.token;
219+
const token = socket.handshake.auth?.token; // Safe access to token
220220

221221
try {
222222
// Authenticate user
@@ -356,6 +356,7 @@ io.on('connection', async (socket) => {
356356
// Handle disconnection
357357
socket.on('disconnect', () => {
358358
onlineUser.delete(user._id.toString());
359+
io.emit('onlineUser', Array.from(onlineUser)); // Inform others of user disconnection
359360
console.log('User disconnected:', socket.id);
360361
});
361362
});
@@ -364,3 +365,4 @@ module.exports = {
364365
app,
365366
server
366367
};
368+

0 commit comments

Comments
 (0)