Giter Site home page Giter Site logo

Comments (3)

iUngerTime avatar iUngerTime commented on September 27, 2024

Faced a few of these, never had one that wasn't my fault.

How can I get more information about this?

Logging! Set up logging on all events for both client and server to view how things are connecting and disconnecting.

I can't answer the question for your use case, because you didn't add any information about it, but I can share my experience. Just from your comment, the fact that the client is reporting the 'EVENT' twice, tells me that your server is sending it twice OR you set up 2 listeners on your client.

Server is sending twice case-scenario:

This could happen if you're trying to build state into your connections. Meaning if they're supposed to receive the event under a certain scenario so you end up attaching multiple listeners server-side. Logging all events on your server and who called it will help you catch these.

This is not dart code, but this should give you an idea of what you could do on your server.

this.io.on(CLIENT_EVENTS.SESSION.CONNECTION, (socket) => {
  new SessionHandler(io, socket);
  new LobbyHandler(io, socket);
  
  console.log(`User ${(socket as any).username} (${(socket as any).userId}) connected with socket id: ${socket.id}`);
  
  // Log all events when received by the server
  socket.onAny((eventName: string, ...args: any) => {
      console.log(`Websocket event: ${eventName} called by ${(socket as any).username} (${(socket as any).userId}) under socket id: ${socket.id}`);
  
      //console.log("Args given: ");
      //console.log(args);
  });
  
  socket.on('disconnect', () => {
      console.log(`User disconnected: ${(socket as any).username} (${(socket as any).userId}) with socket id: ${socket.id}`);
  });
});

Multiple client listeners

Lets say you're navigating to a page, and then you want to listen for the messages sent in a group chat, so that you can print new messages. If you navigate there, and call _socket.on('message-sent', _someEventHandler); but DON'T call _socket.off('message-sent', _someEventHandler);, you will end up attaching 2 event listeners. It's also important to note that calling off, will only remove the event once. If you look at the code for it, it's called "remove" and not "removeAll".

Event handlers are a list of events handlers attached to events. It's not right to say that only 1 action should happen for every event, you might want multiple things to happen at once. Always need to keep that in mind.

from socket.io-client-dart.

senkop avatar senkop commented on September 27, 2024

well you maybe using cubit with io socket
so you are displaying chat list but before it maybe like this

enter code here    Expanded(
        child: BlocBuilder<MessageCubit, List<Message>>(
          builder: (context, state) {
            if (state.isNotEmpty) {

so here you are adding everytime all list of messages not the last one so here the state contain all the list

so when you need to add try like this
messages.add(state.last);

from socket.io-client-dart.

WissamALSbenaty avatar WissamALSbenaty commented on September 27, 2024

I am having the same issue because I had reinitialized the socket class which is repsonsiblr for makeing connection
the solution was making this class so the connection will be only once

from socket.io-client-dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.