question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Socket connects and disconnects multiple times for a same user

See original GitHub issue

When the socket get created, same client gets connected and disconnected to the server multiple times.

Server Side Log image

Client Side Log image

Server code

const httpServer = require("http").createServer();
const io = require("socket.io")(httpServer);

io.on("connection", (socket) => {
  console.log("Connected",socket.id)
});

httpServer.listen(3030);

Client Code

    import React from 'react';
    import { io } from "socket.io-client";
    
    class Room extends React.Component{
      componentDidMount = () =>{
        const socket = io("http://localhost:3030/");
      }
      render(){
        return (
          <div className="room">
            <div className="container-fluid">
              <label className="subHeading">The Notorious mma</label>
            </div>
          </div>
        );
      }
    }
    export default Room;

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
1337-ishaancommented, Jun 17, 2021

@ghirihari pls check out this repo, for reference

private-chat-react

2reactions
1337-ishaancommented, Jun 16, 2021

try replacing your socket declaration to

const socket = io(URL, { autoConnect: false });

This will basically not force a user to keep on connecting

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why socket.io returns multiple times for an event
For broadcast use socket.broadcast.emit(). I don't find any socket disconnection listen event code in your client side code.. try adding ...
Read more >
Fullstack2021 | p17 | SocketIO connect and disconnect
In this series we will build a Web application with realtime communication using NestJS (Backend):https://nestjs.
Read more >
any workaround on multiple socket.io connection after reconnect
At client side will socket.disconnect() removes all the connections? will following code works to remove multiple connection after reconnect?
Read more >
Client API - Socket.IO
Both the client and the server must use the same revision in order to understand each other. You can find more information here....
Read more >
How to prevent multiple socket connections and events in React
After that, I experimented on it a bit, and found out that those listeners are still there even after the connection is closed,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found