If I have multiple pages that could use multiple hub classes, what is the best way to manage this?
For instance:
Is it bad to navigate to another page in the website and essentially "reopen" the connection to the same hub class that was open on the previous page?
Am I correct in thinking that opening multiple hub connections on a page is ok because they are all unified in one connection, even if they are different hub classes?
In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).
What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.
Objective: Use SignalR for notification between Web API, and TypeScript/JavaScript based Web App, where Web API and the Web App is hosted in different domain. Enabling SignalR and CORS on Web API: Create a standard Web API project, and install the following NuGet packages: Microsoft.
SignalR uses the new WebSocket transport where available and falls back to older transports where necessary. While you could certainly write your app using WebSocket directly, using SignalR means that a lot of the extra functionality you would need to implement is already done for you.
You can have multiple hubs sharing one connection on your site. SignalR 2.0 was updated to handle multiple hubs over one signlar connection with no lost in performance.
Official docs: http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server#multiplehubs
All clients will use the same URL to establish a SignalR connection with your service ("/signalr" or your custom URL if you specified one), and that connection is used for all Hubs defined by the service.
There is no performance difference for multiple Hubs compared to defining all Hub functionality in a single class.
To start with Hubs read the WIKI entry for Hubs and Client Side of Hubs. There are couple of things according to the context of a multiple pages.
Example:
You have a page that has two parts, a graph which shows real time user activity and an area to see real time data changes done by users as a table. Will you create two hubs or two groups or what? There are other pages which use same graph and data table.
My Solution:
When you switch between pages the client will connect with same hub and request getGraph or getDataTable or both and populate its client with relevant data. Similarly on server when data changes you can call client side method to update all clients or group of them (lets add this complexity)
Assume you have students and teachers looking at your application. They require different level of data access. You can use groups to keep them separate on the hub so you are not sending teachers info to students and students data to teachers.
Coming back to your question of "is it bad" and "is ok" this is difficult to establish without context of actual application. I cant think of a scenario where you can justify multiple hubs apart from Performance.
Unfortunately this is not possible anymore in the new 'Core' version of SignalR
https://github.com/aspnet/SignalR/issues/456
https://github.com/aspnet/SignalR/issues/955
On iOS there's a limit of FOUR connections per server.
Now there isn't this limit for websockets (I think it may be 32 but not sure). However I am using a self signed certificate which has all kinds of issues in Safari - so it actually drops down to long polling (and it's not obvious it's done so).
So I ended up with these connections:
So if I had ONLY three hubs the whole Safari page would lock up with a blue bar. Even Web API calls got blocked.
Note: With HTTP/2 this limit is gone but you're probably better off limiting yourself to one hub especially if you're using hot reload. Plus setting up HTTP/2 in development isn't necessarily a trivial task.
First (temporarily) set your hub to only accept websockets. This will give you an error in Safari (make sure errors are being caught and shown in an alert dialog).
routes.MapHub<SignalRHub>("/rt", options =>
{
// when run in debug mode only WebSockets are allowed
if (Debugger.IsAttached) {
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
}
});
Now you'll be able to confirm the fix - run in debug mode, or remove the 'if'.
The problem with iOS is even if you accept a self signed certificate for https traffic - and get a nice little 'lock' symbol in the browser - it doesn't apply to the wss: protocol. So connections cannot be upgraded to wss, which is why they block at the max of 4.
If you can get everything down to one hub it's just easier :-)
I also realized that multiple hubs complicates reconnect logic if the connection is lost. One hub just makes this easier. If you're not careful you'll end up showing 3 dialog boxes saying 'Connection lost. Retry?' I'm switching to a single hub just because of this.
While I hate mixing everything, partial classes help and I personally don't have many SignalR methods anyway.
This is only relevant to debugging, and assumes you're using a https cert which you self-signed.
Use instead something like letsencrypt - or Cloudflare's argo tunnel to get a publically trusted cert. This will be fully trusted by Safari, so your connections will get upgraded to real web sockets.
Create a self signed ROOT certificate (CA) and then generate SSL certificates with the domain name from it.
This was trickier than I imagined. In the end it turned out I was missing Subject Type=CA
in my root cert - which iOS requires. Without this 'extension' it will install your root the certificate as a profile, but won't allow you to select it for SSL.
Once you have the root cert installed Safari will work with websockets just fine.
Use http only. This wasn't an option for me because I use certain APIs like Facebook / Google / Payment and they require https.
Better to use one hub in the first place. BUT also best to get your cert installed properly so iOS will work with websockets.
How to create and install X.509 self signed certificates in Windows 10 without user interaction?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With