SignalR1.0.1 - Maintain connectionId - asp.net

I'm using SignalR 1.0.1 and I want to maintain connection id even on page refresh.
Yes, I know, this is a common question of signalr so I got good answered post also but in all of them they mentioned "IConnectionIdFactory" interface which is missing in my app.
1) https://stackoverflow.com/questions/9518394/signalr-maintaining-user-connection-ids
2) http://www.kevgriffin.com/maintaining-signalr-connectionids-across-page-instances/
Before this I was using SignalR alpha version and in that I have manage this issue using "IConnectionIdPrefixGenerator". But in new version this interface is also missing.
Can anyone guide me How to fix this issue proper way?

There is no longer any recommended pattern for setting or maintaining the connection id of a SignalR client in 1.0. I would suggest making single user groups in lieu of ConnectionIds.
SignalR 1.0 beta connection factory
If you don't need to use a custom string to address messages sent to a client and you can trust your clients, you can include metadata inside the query string of each SignalR request.
How to parameterize a SignalR route?

Related

How Can the `id` Query String Parameter in a Blazor WebSocket Connection Be Accessed?

When debugging with my Blazor server-side application, I see the following message:
[2022-02-25T10:40:19.177Z] Information: WebSocket connected to wss://<host>/_blazor?id=<_identifier_>.
I am interested in the id query string parameter specifically. Is this value available in any way within a Blazor application? I looked in the ComponentHub.Context.ConnectionId but it does not appear to match this value.
I do realize that I can use the IHttpContextAccesssor, but want to stay away from this as this is considered poor practice with Blazor applications.
(If I am mistaken about this particular scenario that would be great to know as well.)
This is an internal implementation detail of the SignalR handshake, and it is absolutely not meant for the developer to do anything with it.
For instance, if a websocket connection can't be established, because of networking issues, SignalR falls back to long polling, and each long polling request will get a new ID query parameter, which internally all map to the same SignalR connection id.
See also this great article why this is a bad idea: https://consultwithgriff.com/signalr-connection-ids/

Blazor WebAssembly - use SignalR or Controller actions for basic operations?

I normally develop with ASP.NET MVC, but I'm very new to Blazor. I'm creating a new site in Blazor WebAssembly. The very first thing I need to do is create a page with a simple form, that can create or update an item and send it to the server, to be saved in the DB. I can either send the object using SignalR, or use HttpClient to post it to a controller action. What's the best practice here in Blazor Wasm? I was tempted at first to just use SignalR all the time.
I've seen examples of using both, but very little to help decide which to use in what circumstances. This was about the most useful thing I could find but it doesn't answer the exact question and it's also not specific to Blazor.
The question is specifically about the simple create update operation, but other pros and cons of both would be very helpful. Is it as simple as "only use SignalR when clients need to listen for messages from the server, to avoid having too many open connections"?
Thanks a lot
Is it as simple as "only use SignalR when clients need to listen for messages from the server, to avoid having too many open connections"?
Yes, I think it is. CRUDL operations are transactional and asynchronous. Do a transaction, wait forever on the user, do another transaction,.... I would always do these through an API Get/Post.
The only time I would consider SignalR is where I'm passing object defined objects - such as a Dictionary<string, object>. They are a pain in controller API calls.

SignalR 2.2.2 Asp.Net 4.5 Web Application - Don't have access to the response object in Hub class

I'm using SignalR 2.2.2 Asp.Net 4.5 Web Application. I want to access the current http response object of the client caller but don't know how. How do I reference the response object? I've tried Context.Request.GetHttpContext().Response but it errors out saying "It does not exist in the current context". Can someone please assist and point me in the right direction.
SignalR does not let the user write directly to HttpResponse Stream for a couple of reasons and even if you found a way to do this it is not a good idea:
when using WebSockets transport SignalR uses webSockets to send messages so writing to HttpResponse would not work (and is not possible)
SignalR uses its own JSON based protocol if you write anything directly you will most likely create invalid SignalR messages which the client will not be able to process
If you want to send PDFs over SignalR you will need to be able to save it to a stream (e.g. MemoryStream) and invoke the client with an array created from this stream. (Note that the version of SignalR you are using is using JSON based protocol so sending binary data will be quite inefficient - it might be better to use SignalR to let the client know there is a PDF they can download and then the client would download it using e.g. HTTP GET request)

Web sockets .net 4.5 how to get a reference to a connection

I am new to websockets in asp .net, I have started building a simple chat application
I can open a connection using http handler I can send and receive data but my question is: how can I access other client connection from some handler so that I can send something
I have searched a lot for something similar and I have found this
http://demos.html5support.nl/WebSockets/SimpleChat
I would like to do exactly like in this page but here they are using a non-standard library, I want to do this using standard websocket support in .net 4.5
Any help is appreciated
Take a look at this example : How to keep track of established connections using WebSockets
Basically you need to keep track of the WebSocket objects and associate them with an ID or something in a shared collection. When you want to send something, just retrieve the connection and use it.

HttpSession Session ID different to FlexSession ID

I have a Flex application which is served via a JSP page. In this page I output the session ID using HttpSession when the page is loaded:
System.out.println("Session ID: " + session.getId());
In a very simple remote object hosted in BlazeDS (called from the flex application using an AMF Channel and standard RemoteObject functionality) I also output the session ID but this time using FlexSession (which as I understand is supposed to wrap around HttpSession).
System.out.println("FlexSession ID: " + FlexContext.getFlexSession().getId());
I would expect both IDs to be the same but this is not the case. The session IDs differ which is causing problems as there is data stored in the HttpSession which I need to be able to access from my remote objects within BlazeDS.
I've exhausted the reading material on BlazeDS and FlexClient/FlexSession/FlexContext but can't see why the FlexSession is not being linked to the HttpSession. Any pointers greatly appreciated.
I feel I must be missing something fundemental here, am I accessing the
I do not think that it is related to the FlashPlayer..is more related to the concept of FlexSession and how BlazeDS/LCDS works. For example you can have an active session even when not using the http channels - when using NIO/RTMP you are bypassing the application server and the http protocol. So it make sense to have an abstract class FlexSession with various implementations.
However when using BlazeDS FlexSession will wrap an HttpSession object internally, and removeAttribute/getAttribute/setAttribute are in fact calling the the same methods from the HttpSession object..so you can access all the data from the HttpSession. If not please provide more details.
However, it will not work when using RTMP channels(which exists only in LCDS by the way), you need to change your design in this case.
Thanks to both answers above I finally found the root cause and thought I'd share it on here.
The reason for differing session IDs was to do with the use of SSL for authentication and the use of AMF Channel rather than Secure AMF. Using the channel for the first time caused a new session to be created (hence the different ID) as the existing session related to the secure version of the site.
Silly configuration mistake but worth passing on - make sure that if using SSL that you're also using Secure AMF connecting to the secure endpoint rather than standard AMF or you'll run into the same session ID problems I faced.
Unfortunately this is just how the Flash player works. I have seen this same behavior many times.
The best solution I found was to establish the HTTP session and pass back the session ID. On the client side, you can then pass the session ID to the Flex application. You then send that ID from Flash to the server and use it to look up the existing session or establish a second session.
You will need to do something like this though, I have not been able to find a way to reliably get Flash to use the same session.

Resources