Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

receiving duplicate message #381

Open
yalda-student opened this issue May 27, 2024 · 3 comments
Open

receiving duplicate message #381

yalda-student opened this issue May 27, 2024 · 3 comments

Comments

@yalda-student
Copy link

Hello. I use this package in my flutter app, I faced a problem. Sometimes when user is sending message through socket, they received duplicate messages; actually socket.on('EVENT', (data){}) is performed 2 times. after restarting app, this bug fixed.
How can I fixed it? is it a server side bug or client side? How can I get more information about this?
Thanks

@iUngerTime
Copy link
Contributor

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.

@senkop
Copy link

senkop commented Jun 23, 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);

@WissamALSbenaty
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants