How to capture SignalR native client losing connection with server - signalr.client

I am working on a .net client application which utilizes SignalR. I want to notify the user if the connection is down for some reason. How can I capture the disconnect event using the native client?

The client will go into the reconnecting when the connection dies.
Therefore you can tie into the Reconnecting event to see when the connection goes down:
var connection = new Connection("http://myEndPointURL");
connection.Reconnecting += () =>
{
Console.WriteLine("The connection has gone down, shifting into reconnecting state");
};
Hope this helps!

I was able to capture StateChanged to detect changes in the connection and notify the user.
connection.StateChanged += (statechange) =>
{
Console.WriteLine("Changing from " + statechange.OldState + " to " + statechange.NewState);
};
This gives me a mechanism to notify the user when the connection is dropped or successfully reconnected.

Related

React Native Signalr keeps saying connection needs to be initialized

I have this in my code:
useEffect(() => {
if(loaded2 == false){
connection = signalr.hubConnection('https://webambe.com/xxx');
connection.logging = true;
proxy = connection.createHubProxy('xxx');
//receives broadcast messages from a hub function, called "helloApp"
proxy.on('updateAll', (argOne) => {
setAlertNum(1);
//Here I could respond by calling something else on the server...
});
// atempt connection, and handle errors
connection.start().done(() => {
//console.log("dddd" + proxy.connectionState);
setLoaded2(true);
//console.log('Now connected, connection ID=' + connection.id);
setTrans(false);
}).fail(() => {
//console.log('Failed');
});
//connection-handling
connection.connectionSlow(() => {
//console.log('We are currently experiencing difficulties with the connection.')
});
connection.error((error) => {
const errorMessage = error.message;
let detailedError = '';
if (error.source && error.source._response) {
detailedError = error.source._response;
}
if (detailedError === 'An SSL error has occurred and a secure connection to the server cannot be made.') {
//console.log('When using react-native-signalr on ios with http remember to enable http in App Transport Security https://github.com/olofd/react-native-signalr/issues/14')
}
//console.debug('SignalR error: ' + errorMessage, detailedError)
});
}
});
When the code hits connection.start().done((), then the connection should be established but when I hit the send button that invokes the hub i get this error (setLoaded2(true) means that the connection is established):
Connection has not been fully initialized. Use .start().done() to run the logic after
connection has started.
But I have stated that says that connection has started here:
setLoaded2(true);
When this is called setTrans(false) is called to get rid of the transparent view so users can interact with the UI.
The hub connects successfully and I can run commands to the server and back, but I just want to know how to say when the connection is established so that the user can use the system.
I'm not sure if this is the way it shud be done but its working and staying connected when I move the connection and proxy outside my functional component:
connection = signalr.hubConnection('https://webambe.com/xxx');
connection.logging = true;
proxy = connection.createHubProxy('xxx');
when added this code outside the component the connection stays connected and no issues with signalr

Janus gateway videoroom cancels connection after 60 seconds

"peerConnection new connection state: connected"
{
"janus": "webrtcup",
"session_id": 3414770196795261,
"sender": 4530256184020316
}
{
"janus": "media",
"session_id": 3414770196795261,
"sender": 4530256184020316,
"type": "audio",
"receiving": true
}
... 1 minute passes
"peerConnection new connection state: disconnected"
{
"janus": "timeout",
"session_id": 3414770196795261
}
"peerConnection new connection state: failed"
See pastebin for the full logs.
I'm trying to join a videoroom on my Janus server. All requests seem to succeed, and my device shows a connected WebRTC status for around one minute before the connection is canceled because of a timeout.
The WebRTC connection breaking off seems to match up with the WebSocket connection to Janus' API breaking.
I tried adding a heartbeat WebSocket message every 10 seconds, but that didn't help. I'm
joining the room
receiving my local SDP plus candidates
configuring the room with said SDP
receiving an answer from janus
accepting that answer with my WebRTC peer connection.
Not sure what goes wrong here.
I also tried setting a STUN server inside the Janus config, to no avail. Same issue.
Added the server logs to the pastebin too.
RTFM: Janus' websocket connections require a keepalive every <60s.
An important aspect to point out is related to keep-alive messages for WebSockets Janus channels. A Janus session is kept alive as long as there's no inactivity for 60 seconds: if no messages have been received in that time frame, the session is torn down by the server. A normal activity on a session is usually enough to prevent that; for a more prolonged inactivity with respect to messaging, on plain HTTP the session is usually kept alive through the regular long poll requests, which act as activity as long as the session is concerned. This aid is obviously not possible when using WebSockets, where a single channel is used both for sending requests and receiving events and responses. For this reason, an ad-hoc message for keeping alive a Janus session should to be triggered on a regular basis. Link.
You need to send 'keepalive' message with same 'session_id'to keep the session going. Janus closes session after 60 seconds.
Look for the implementation: https://janus.conf.meetecho.com/docs/rest.html
Or do it my way: i do it every 30 seconds in a runnable handler.
private Handler mHandler;
private Runnable fireKeepAlive = new Runnable() {
#Override
public void run() {
String transactionId = getRandomStringId();
JSONObject request = new JSONObject();
try {
request.put("janus", "keepalive");
request.put("session_id", yourSessionId);
request.put("transaction", transactionId);
} catch (JSONException e) {
e.printStackTrace();
}
myWebSocketConnection.sendTextMessage(request.toString());
mHandler.postDelayed(fireKeepAlive, 30000);
}
};
Then in OnCreate()
mHandler = new Handler();
then call this where WebSocket connection Opens:
mHandler.post(fireKeepAlive);
be sure to remove callback on destroy
mHandler.removeCallbacks(fireKeepAlive);

SignalR Connection fails in Iphone safari browser gets minimized. How to make it stay connected ? or How to Reconnect when Tab gets active

I am using Signal-R Core, a simple chat page. I logged in with different clients like browser in laptop, mobile browser both Android and IOS,
I could able to send message to all clients, But when mobile browser(only IOS) gets minimized Signal-R connection disconnected,
I've tried implementing a logic to reconnect. Idea is to set a flag on connection.close() get called, And then starting the connection by calling connection.start() on Window.focus() get called. Window.focus() function is working properly.
This works as 1 after 1 time the browser minimized, Only on iPhone browser.
Scenario : Initially connected, message sent successfully.
Now browser minimized, and then opened again and sending message from iPhone client, now gets failed.
Then again minimized and opened again the browser, sending from iPhone client, now message sent successfully.
Tried it multiple times. works same.
var flagConnection = true;
var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
connection.on("BroadcastMessage", function ( user, message) {
var encodedMsg = user + ": " + message;
var li = document.createElement("li");
li.textContent = encodedMsg;
$("#divChatMessages").append(li);
});
connection.start();
connection.onclose(function(change) {
if(connection.connectionState == 0)
flagConnection = false;
else
flagConnection = true;
});
window.addEventListener("focus", function(event)
{
if(flagConnection == false){
connection.start();
flagConnection = true;
}
});

Websocket Client in Electron App delaying reception

I am using a websocket to push data from a process (running in the background) to my electron application (renderer, it's a electron-vue app). Mostly this works great, the data is received and displayed instantly.
In some cases, however, I noticed that the websocket client seemed to buffer incoming messages and only trigger the receive-event after some delay, leading to messages received as a batch.
To verify that the server isn't buffering anything I ran a second connection and simply logged the data (chrome-addon), there all the data is received and processed instantly while my electron application delays the messages.
I am using ReconnectingWebsocket but also tried a plain websocket application:
let webSocket = new WebSocket('ws://0.0.0.0:7700')
webSocket.onopen = function(openEvent) {
console.log('WebSocket OPEN: ' + JSON.stringify(openEvent, null, 4))
}
webSocket.onclose = function(closeEvent) {
console.log('WebSocket CLOSE: ' + JSON.stringify(closeEvent, null, 4))
}
webSocket.onerror = function(errorEvent) {
console.log('WebSocket ERROR: ' + JSON.stringify(errorEvent, null, 4))
}
webSocket.onmessage = function(messageEvent) {
var wsMsg = messageEvent.data
console.log('WebSocket MESSAGE: ' + wsMsg)
}
The WebSocket MESSAGE: is only displayed with some delay. Is there any configuration option, like buffering on the client side or must the render process be called more often..?
Not sure of the solution but we have a demo app using Vue + Electron at https://github.com/firesharkstudios/butterfly-server-dotnet/tree/master/Butterfly.Example.Todo that also uses WebSockets. I've never seen a delay or buffering like you are seeing. Maybe you can compare the implementations to find a cause.
It turns out that it wasn't the websocket implementation but electron blocking the renderer process completely while scrolling, thus the delayed reception. I had to move the websocket connection out of the renderer and tunnel all messages using the IPC system.

Paho Rabitmqq connection getting failed

Here is my paho client code
// Create a client instance
client = new Paho.MQTT.Client('127.0.0.1', 1883, "clientId");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect});
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("/World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "/World";
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
On Rabbitmq server everything is default seetings. When i run this code i get WebSocket connection to 'ws://127.0.0.1:1883/mqtt' failed: Connection closed before receiving a handshake response
What i am missing ?
From my personal experience with Paho MQTT JavaScript library and RabbitMQ broker on windows, here is a list of things that you need to do to be able to use MQTT from JS from within a browser:
Install rabbitmq_web_mqtt plugin (you may find latest binary here, copy it to "c:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.2\plugins\", and enable from command line using "rabbitmq-plugins enable rabbitmq_web_mqtt".
Of course, MQTT plugin also needs to be enabled on broker
For me, client was not working with version 3.6.1 of RabbitMQ, while it works fine with version 3.6.2 (Windows)
Port to be used for connections is 15675, NOT 1883!
Make sure to specify all 4 parameters when making instance of Paho.MQTT.Client. In case when you omit one, you get websocket connection error which may be quite misleading.
Finally, here is a code snippet which I tested and works perfectly (just makes connection):
client = new Paho.MQTT.Client("localhost", 15675, "/ws", "client-1");
//set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
//connect the client
client.connect({
onSuccess : onConnect
});
//called when the client connects
function onConnect() {
console.log("Connected");
}
//called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
//called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
}
It's not clear in the question but I assume you are running the code above in a web browser.
This will be making a MQTT connection over Websockets (as shown in the error). This is different from a native MQTT over TCP connection.
The default pure MQTT port if 1883, Websocket support is likely to be on a different port.
You will need to configure RabbitMQ to accept MQTT over Websockets as well as pure MQTT, this pull request for RabbitMQ seams to talk about adding this capability. It mentions that this capability was only added in version 3.6.x and that the documentaion is still outstanding (as of 9th Feb 2016)