What's the correct way to handle state in a SignalR Hub?
I am going to implement a SignalR hub which will be connected to by a client application. The purpose of this being SignalR and not just HTTP requests is so that I can send messages from the server back to the client at any point without the need for something like port forwarding, just ad-hoc, and I will also use the connection to request general data too, so I don't need more than one connection to communicate data either direction.
I would like to authenticate the client with an API key when it connects, or allow it to connect unauthenticated so I can pop it up on my control panel and assign it a new API key and identity remotely (remote provisioning, essentially)
What would the correct way to do this be? I understand that sessions are not available as a result of "duplex messaging" which makes perfect sense, so my other idea was to do this with a scoped service to hold some state data for the connection, but as far as I can tell, the DI scope is not guaranteed especially if the hub is connected via HTTP polling instead of WS.
24 Replies
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
That handles with but not state data
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
I stuck this on Reddit too and someone said Context.Items but not 100% certain if that would work with polling
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
That would be fine, I'd like it per connection, they would have to set up again on reconnection
This isn't users, it's autonomous
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Sure
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
And how about Context.Items for simple data, is that viable or not?
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Hm so singleton might be best then with ID
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Unlike a Controller (which IIRC is Scoped) is the hub itself a Singleton then?
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Ah ok
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
How does the hub store a connection list if it is transient?
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Yes
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Ah right
This is more complex than I thought
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Nwrs, thanks for your help