C
C#3mo ago
Pokey

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
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
That handles with but not state data
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
I stuck this on Reddit too and someone said Context.Items but not 100% certain if that would work with polling
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
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
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
Sure
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
And how about Context.Items for simple data, is that viable or not?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
Hm so singleton might be best then with ID
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
Unlike a Controller (which IIRC is Scoped) is the hub itself a Singleton then?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
Ah ok
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
How does the hub store a connection list if it is transient?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
Yes
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
Ah right This is more complex than I thought
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pokey
PokeyOP3mo ago
Nwrs, thanks for your help
Want results from more Discord servers?
Add your server