not connecting socket.io nextjs app with nodejs app #4933
-
Hello everybody, // Create a socket connection
const socket = io("http://localhost:8000", {transports: ["websocket"]});
socket.on("connection", () => {
console.log(socket.id);
});
socket.on("disconnect", () => {
console.log(socket.id); // undefined
}); Server const httpServer = require("http").createServer();
const io = require("socket.io")(httpServer, {
cors: {
origin: "http://localhost:4010",
}
});
// function that handles socket.io connections
io.on('connection', (socket) => {
// on connect print user connected msg
console.log('A user connected.');
// on socket disconnect
socket.on('disconnect', () => {
// print user disconnect msg
console.log('A user disconnected.');
});
// Handle custom 'hello' event
socket.on('message', (data) => {
console.log('Server Received hello event:', data);
// Emit a response event
socket.emit('response', 'Hello, client!');
});
}); Edit by @darrachequesne: code formatting |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
That's weird! If you go to http://localhost:8000/socket.io/?EIO=4&transport=polling, does it reach the Socket.IO server? Reference: https://socket.io/docs/v4/troubleshooting-connection-issues/#troubleshooting-steps |
Beta Was this translation helpful? Give feedback.
That's weird! If you go to http://localhost:8000/socket.io/?EIO=4&transport=polling, does it reach the Socket.IO server?
Reference: https://socket.io/docs/v4/troubleshooting-connection-issues/#troubleshooting-steps