Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of messages in SingalR - how to ensure it

I'm using SingalR in an application that sends alot messages in a short period of time. let's say i have client A and client B. Client A just sends messages and client B just listening to messages. Client A sends the following messages in the following order: A->B->C->D

What i'm seeing is that Client B sometimes receives the messages in a different order, for example: B->A->C->D

It is important for maintain the same order i sent the messages. I've looked online and i found people saying i should use async-await on the function on the hub that handles those messages.

public async Task hubMethod(msgObject msg)
{
   await Clients.All.message(msg);
}

I'm not sure how that helps since each time i make a call from client A , singalR should create a new instance of the hub. The only thing it does is wait for the singalR that it finished doing all it can do on the server in order to send the message to the other client and notifies it to client A.

So my question is this - is there a singalR or asp.net mechanism that make sure i receive the messages in the correct order on the other client or do i need to write my own mechanism (server or client) that reorders the messages if they are out of order - and if so, is there a library that already does it?

like image 827
Sandman Avatar asked Jan 26 '17 08:01

Sandman


Video Answer


1 Answers

You need to write your own mechanism. SignalR in client B has no way to know in which order the clients messages were sent by client A because there is many things that could delay a specific message arrival, like network delay, the only thing SignalR can guarantee is the order in which the messages arrived.

If you really need to know the original order of the messages you could put a count inside the message and let client B sort them out. However i suggest you try another approach, because guaranteeing the order of delivery is not a easy task.

like image 108
Leonardo Menezes Avatar answered Nov 07 '22 15:11

Leonardo Menezes