Exception when sending message to disconnected client in SignalR - signalr

In a Hub, I am sending a message to a client using
try {
GlobalHost.ConnectionManager.GetHubContext<ContextManagerHub>().Clients.Client(connectionID).SendMessage(message);
} catch(Exception ex) {
HandleError(ex);
}
The connectionId is stored using OnConnected and removed using OnDisconnected on the Hub.
I am a little worried that I am sending messages to disconnected clients, and would like to get exceptions if I do. But the code above never raises exception, even if I try using connectionID = "BAD_ID". Anyone got an idea on how to figure out if the SendMessage actually fails?

So SignalR does not actually validate if your sending to an invalid ConnectionId. The reason why this does not occur is because ConnectionId's for clients are a lot like SignalR groups. They reference a topic that can be subscribed and published to.
Therefore when sending to an invalid ConnectionId you push a message to a topic that doesn't exist yet; so it's created. The reasoning behind this is if a client happens to come to the server with that connection id via a reconnect or by other means they will then receive any messages that they had missed during their down time.
Sooo for your case I'd recommend tracking clients via the OnConnected and OnDisconnected methods and then running logic to verify that they are present prior to sending to them.

Related

Does Signalr change connectionId OnReconnected method?

I was wondering if signalr changes the connectionId on OnReconnected method? as I'm mapping a list of users to their connection ids so if the reconneced event raised on the server due to slow connection for example will the connectionId will be changed ?
No. The connectionId does not change in an OnReconnected event.
The OnReconnected fires automatically(if you're using a signalR client) within the disconnect timeout in case there is a network issue and a keep alive is missed.
If the network is disrupted or the server is unable to process the OnReconnected event for longer than the disconnect timeout, the connection will be disconnected on the server and a new connection will need to be made. This making a new connection is not automatic. The client code will have to call a connection.Start() to create a new connection.
If the client comes back with the old connectionId, it will not go through as that connection would be in a disconnected state.
So if you are mapping connectionIDs with Users then you need to make sure that the connection Id is updated with the new connectionId in every OnConnected event and deleted in every OnDisconnected event on the server .
I don't believe the OnReconnect will change the connectionID. However, that depends on what the cause of the problem is.
If the client dropped it's internet connection for example and then picked back up, OnReconnect fires and the user should have the same connectionID.
If the server rebooted or IIS restarted, then you have a different situation and when your clients refresh it will appear as a OnConnect and get a new connectionID.

Timeout for broadcasting message using SignalR

I am using SignalR for the 1st time in my asp.net c# web app.
I am using HTML 5 and JavaScript for my client web page.
In essence whenever my server has an image to 'push' to my clients it does so. Sometimes this can be quite frequent.
Now, I imagine during a 'busy' period' my client(s) could be 'over-whelmed' by the data being pushed by my server?
How does my server know that the message has been recieved by my client (or not) and then proceed to send the next one?
SignalR doesn't provide a built-in way to wait for a client to receive a message. This would be very difficult to do in the general case where someone might be using Clients.All, and clients might be connecting/reconnecting/disconnecting at the time the message is sent.
However, it is possible to manually acknowledge that you have received a message on the client (perhaps by calling some hub method on the server), and then continue to send the next message once the acknowledgement (ACK) for the previous one has been received.

Implementing a robust and efficient RPC system

I need to have a server which is able to call functions on the client. I always used RPC's in different networking game API's but I never implemented it by myself.
How would I do it?
My naive approach would be:
connect client to the server:
server
fn update_position_client(){
unique_id = 1;
send.to_client(unique_id);
}
client
while recv_messages {
if id == 1
update_position();
}
Is this how I would do it?
This works if you only have a few messages that you want to send, and if the data basically known. To be more robust, you would want to have the ability to dynamically add/remove messages that can be called, and figure out how to look up the methods to be called when RPC is called.
Assuming you want this to be completely transparent to the user, what typically happens in this case is that when the a message is sent, the RPC library will wait until there's a response back. Assuming bi-directional capabilities, what normally happens is that there's a single thread that listens for data. If an RPC message comes in, this thread will figure out what to do with the message, i.e. what method to call in your(local) address space and with what parameters you want to call it with. When you send an RPC message out, the thread that you sent the message out on is blocked(probably with a semaphore) until the return message comes back, at which point your local thread is unblocked and allowed to continue.
A Linux-specific RPC library you could look at would be DBus.

blazeds how to understand if the session is timed out

Hi my aim is to catch when the session is invalidated and send message to all destination that the FlexClient subscribed that the client wiht id .... has disconnected.
I implemented a service which listens for sessionCretaed,sessionDestroyed,clientCreated,clientDestroyed,messageClientCreated,messageClientDestroyed.
Problem is that i want to catch messageClientDestroyed and send to that destination a message that the user has disconnected.But blazeds destroys messageClient when you add a subtopic.So how to understand that if session is invalidated and all messageClient are being destroyed or the user just enter another place and add a subtopic to his consumer.
I'am also open for other solutions to implement that mechanism.When user disconnected by session timeout or closed the site browser etc. i wanna catch it and send a message to subscribed destinations.
thank you.
I am not a master on this topic but I believe you have to tune into the Flex session lifecycle event, FlexSessionListener after a d/c.
If your client is leaving because they want to, on the flex side you invoke disconnectAll() on the ChanelSet so that Blaze DS will clean up resources explicitly.
If your client is leaving because of timing out, you can configure the channel definition to use invalidate-session-on-disconnect = true
These two should trigger the FlexSessionListner.sessionDestroyed(). The parameter for this method is a FlexSession object where you can extrapolate what you need.

Is Javamail asynchronous or synchronous?

Is Javamail asynchronous or synchronous? That is, if I send off an email, do I continue processing immediately afterwards, or do I wait until it's complete?
Furthermore, are there any ways that I could catch that an email failed to be delivered for any reason?
I'd also like to know these answers for Spring's MailSender abstraction.
Thanks.
It is synchronous, since it transfers the message to the server and processes the server's response before returning. The send docs explain in further detail. The message will throw a SendFailedException, or another MessagingException,
if the send fails immediately. But "success does not imply that the message was delivered to the ultimate recipient, as failures may occur in later stages of delivery."

Resources