Socket Disconnect | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Socket Disconnect

Hello Dear Community I am using socket IO with express (server) react JS (client) user after time of inactivity seems to be disconnected and need to reload to see the new changes I made many searches but no specific solution helped me Also my socket connection is not upgrading to WebSocket ; it stuck in polling ; can this be related to my problem ? Any suggestion will be appreciated Thank you in advance

20th May 2022, 12:40 PM
marilynn
1 Answer
0
Hard to say without seeing your code, but here's an example of how to use WebSocket if that helps. If it isn't using or able to use WebSocket, then it'll default to long-polling and that can certainly cause your issue. Server-side: const express = require('express'); const app = express(); const http = require('http').Server(app); const io = require('socket.io')(http); // ... io.on('connection', (socket) => { // Handle socket connection }); http.listen(3000, () => { console.log('Server started on port 3000'); }); Client-side: import io from 'socket.io-client'; const socket = io('http://localhost:3000', { transports: ['websocket'], }); // ... socket.on('connect', () => { // Handle socket connection });
2nd Jun 2023, 5:59 PM
AgentSmith