Send a message to all active tabs on the browser using SignalR
I'm working on an ASP.NET MVC app using SignalR that implements a WhatsApp Chat, where the user can send a message from the app to a WhatsApp number, and from there it can be answered, thus showing the response on the page. So far, I've got to separate the messages sent from the WhatsApp account to each tab on different browsers, as I'm using the SignalR connectionID
, given that each time a page is loaded, a new connection is established.
From the client, I send the messages as follows
connection.invoke("SendMessage", user, message).catch(function (err) { return console.error(err.toString()); });
and from the server, I send the message to all clients as follows
public async Task SendMessage(string user, string message) { await Clients.All.SendAsync("ReceiveMessage", user, message); }
To a particular client, I send a message with the connectionID
await _hubContext.Clients.Client(idConexion).SendAsync("ReceiveMessage", "Message:", textMessage[0].Text.Body);
What I'd like is that the same browser is considered as only one connection, no matter how many tabs are opened. I know from the documentation and other StackOverflow questions that each tab opened is a different connection, but I just need to broadcast the same message to all active tabs of a browser. My current logic is that messages are sent to a particular connection, using the unique WAMID from each message from the WhatsApp Business API.