6

Why SignalR is making different connection in browser multiple tabs for same logged-in user. Is there any way to make one connection for all tabs opened in same browser. From connection I mean connectionID of a user in SignalR.

Suresh
  • 3,875
  • 24
  • 32
Amna
  • 543
  • 4
  • 25
  • 1
    What else do you expect? Multiple physical connections sharing same connectionID ? – Michal Levý Jun 02 '15 at 05:26
  • 1
    I want same connectionID in multiple tabs of one browser, Like user session remains same in multiple tabs within one browser. – Amna Jun 02 '15 at 06:29
  • Its not possible. Why do you need it ? – Michal Levý Jun 02 '15 at 06:51
  • 1
    I don't want to make multiple connections for one physical user, whose session in my application is only one. If i bound user to open multiple tabs, then that would be an irritating restriction. On the other hand SignalR multiple connections for one user is costly for me. Assume 15 opened tabs within same browser mean 15 concurrent signalR connection, but physical user is only one. – Amna Jun 02 '15 at 06:56
  • But different tabs **are** different instances of your app (client side) even if they share same session on server. – Michal Levý Jun 02 '15 at 07:19
  • 6
    I get tired of this mentality in responses to straight-forward questions. "What else do you expect?" or "Why do you need it?" It's very obvious: same user, multiple tabs. Clients.User(xyz) should go to the user, not a tab. As Diana mentioned: sessions, for example, work this way. I get why this is not possible, but no need to snarkily reply. I suppose I need to make a "group" per user ID instead of a "user" per user ID. Ugh. – vbullinger May 01 '17 at 20:11
  • @Amna Did you find any solution to this problem? – Kardon63 Apr 14 '21 at 07:37

1 Answers1

4

Because different tabs of the same browser are different documents\"processes" - each tab in some sense represents different "instance" of client app. There is no way to share JavaScript objects between them directly (and thats the reason why SignalR opens new connection in each tab). To share data\communicate between different instances, you can use for example HTML5 localStorage mechanism

So if you want to share one SignalR connection between tabs, the way to go is to implement it yourself by managing connection only in one tab and allowing communication between tabs so that every tab has the ability to send\receive messages from server. You can use or get inspired by this cool project which does exactly that.

This also solves the problem of browser limit of maximum concurrent connections to single server.

Community
  • 1
  • 1
Michal Levý
  • 14,671
  • 1
  • 27
  • 41
  • Why we can not share? It is possible we can share javascript objects in different tabs. If you want to see the process just refer the below link, there open multiple tab and select any answer at the same time every tab data is changed, [http://quizworld99.blogspot.in/2015/10/mental-ability-test-1.html](http://quizworld99.blogspot.in/2015/10/mental-ability-test-1.html) – Merbin Joe Dec 09 '16 at 04:18
  • 1
    1. I tried right now in Chrome and no, answering in one tab doesn't affect another tab. 2. Even if it did worked as you describe, that doesn't implies you can share JavaScript instances between tabs. If you need to share data\communicate between tabs\browser windows, you need to use something like HTML5 localStorage mechanim... – Michal Levý Dec 13 '16 at 08:50
  • thanks for the github link, awesome project. I mean this one https://github.com/slimjack/IWC-SignalR – Alex from Jitbit Sep 06 '17 at 10:38