Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing same connection in signalR while navigating through different pages

I have an MVC project, with multiple pages.

I have a long running process, which updates the client on its progress. However, this update is sent only to a single client, instead of broadcasting to all.

Clients.Client(ConnectionId).sendMessage(msg);

In my layout.cshtml, this is how I connect to the hub

    var serverHub = $.connection.notifier;
    window.hubReady = $.connection.hub.start(function () { });

The problem is, when I navigate to another page, I no longer receive the messages from signalr, because the connection id has changed.

How should I workaround this issue, such that my signalr hub can still send messages to a single client, while the client navigates from page to page.

like image 638
Null Reference Avatar asked Apr 18 '13 08:04

Null Reference


People also ask

How long do SignalR connections stay open?

The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command.

Is SignalR bidirectional?

SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available. SignalR supports Web Sockets, and falls back to other compatible techniques for older browsers.

How many concurrent connections can SignalR handle?

IIS on client operating systems has a limit of 10 concurrent connections. SignalR's connections are: Transient and frequently re-established. Not disposed immediately when no longer used.

Is SignalR two way communication?

SignalR is a two-way RPC protocol (request–response protocol) used to exchange messages between client and server (bi-directional communication) that works independently of transport protocols.


1 Answers

You will want to create a server mapping of users to connection id's. See: SignalR 1.0 beta connection factory.

You will want to let your users persist past an OnDisconnected event and when they connect with a different connection Id you can continue pumping data down to them.

So the thought process could be as follows:

  1. Page loads, SignalR connection is instantiated
  2. Once connection is fully started call => TryCreateUser (returns a user, whether it existed or it was created).
  3. Long Running process Starts
  4. Client changes pages -> SignalR connection stops
  5. New Page loads, new SignalR connection is instantiated
  6. Once connection is fully started see if you have a cookie, session, or some type of data representing who you are => TryCreateUser(userData) (returns the user you created on the last page).
  7. Data continues pumping down to user.

Note: if you take an authentication approach you will have to be authenticated prior to starting a connection and that data cannot change during the lifetime of a SignalR connection, it can only be created/modified while the connection is in the disconnected state.

like image 192
N. Taylor Mullen Avatar answered Nov 16 '22 02:11

N. Taylor Mullen