You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I am using socketio with PM2 and cluster mode.
I am using @socket.io/pm2. Also used socket.io/cluster-adapter and socket.io/sticky s per the docs.
In client side I changed transport to polling (Removed websocket).
With websocket in client side everything is working properly.
When using polling, client connects with server and disconnects.
constfp=require('fastify-plugin')const{ Server }=require("socket.io");const{ createAdapter }=require("@socket.io/cluster-adapter");const{ setupWorker }=require("@socket.io/sticky");asyncfunctionsocketio(fastify,opts={}){console.log("Creating sockio");constio=newServer(fastify.server,opts);io.adapter(createAdapter());setupWorker(io);fastify.decorate("io",io);fastify.addHook("onClose",(fastify,done)=>{fastify.io.close();done();});}module.exports=fp(socketio);
module.exports=(fastify)=>{fastify.ready().then(()=>{fastify.io.on("connection",(socket)=>{console.log(`New client connected via: ${socket.conn.transport.name}`);socket.conn.on("upgrade",()=>{console.log(`Transport changed to ${socket.conn.transport.name}`);});socket.on("message",(msg)=>{console.log("Message received over connection type : ",socket.conn.transport.name);socket.emit("reply","Message received loud and clear!");});socket.on("disconnect",()=>{console.log("Connection disconnected");});});});};
// Connect to the Socket.IO serverconstsocket=io('https://chat.xyz.com',{transports:['polling']});// Connect to the local server// Function to append messages to the message divfunctionappendMessage(message){constmessageDiv=document.getElementById('messages');constnewMessage=document.createElement('div');newMessage.textContent=message;messageDiv.appendChild(newMessage);}// Listen for connection eventsocket.on('connect',()=>{console.log('Connected to server');appendMessage('Connected to server');});socket.on('disconnect',()=>{console.log('Disconnected from server');appendMessage('Disconnected from server');});// Listen for reply eventsocket.on('reply',(msg)=>{console.log('Received reply:',msg);appendMessage(`Reply: ${msg}`);});// Listen for error eventsocket.on('error',(error)=>{console.error('Error received from server:',error);appendMessage(`Error: ${error}`);});// Handle the send message button clickdocument.getElementById('sendMessageButton').addEventListener('click',()=>{constmessage='Hello from client';console.log('Sending message:',message);appendMessage(`Sent: ${message}`);socket.emit('message',message);});
Expected behavior
In polling mode pm2 sticky session should work and client should not get disconnect.
Platform:
Device: mac chrome browser
OS: Mac sonoma
Additional context
With websockets it is working. I am testing with polling for fallback support
The text was updated successfully, but these errors were encountered:
Describe the bug
I am using socketio with PM2 and cluster mode.
I am using @socket.io/pm2. Also used socket.io/cluster-adapter and socket.io/sticky s per the docs.
In client side I changed transport to polling (Removed websocket).
With websocket in client side everything is working properly.
When using polling, client connects with server and disconnects.
To Reproduce
Please fill the following code example:
Versions:
"@socket.io/cluster-adapter": "^0.2.2",
"@socket.io/sticky": "^1.0.4",
"socket.io": "^4.7.5",
Server
Client
https://cdn.jsdelivr.net/npm/socket.io-client/dist/socket.io.js
Expected behavior
In polling mode pm2 sticky session should work and client should not get disconnect.
Platform:
Additional context
With websockets it is working. I am testing with polling for fallback support
The text was updated successfully, but these errors were encountered: