I am experiencing the strangest behavior with SignalR. After some refactoring I encountered issues with connectivity. I think it was pure luck that made it work before as the code didn't comply to the recommended practices. For instance I started the hub before I declared the hub methods (as explained by David Fowler) so the client never explicitly subscribed to those hubs but somehow they did. I have looked all day why and how but no luck.
After refactoring this is the code that is executed once the document is loaded:
function initSignalR() {
var me = this,
appointmentHub = $.connection.appointmentHub,
assignmentHub = $.connection.assignmentHub,
taskHub = $.connection.taskHub,
notificationHub = $.connection.notificationHub,
messageHub = $.connection.messageHub;
$.connection.hub.connectionSlow(onConnectionSlow);
$.connection.hub.stateChanged(onStateChanged);
$.connection.hub.error(onError);
$.connection.hub.disconnected(onDisconnected);
$.connection.hub.logging = true;
appointmentHub.client.updateAppointment = onUpdateAppointment;
$.connection.hub.start().done(onDone).fail(onFailed);
... code ommitted for brevity ...
}
function onUpdatedAppointment(appointment) {
.... code ommitted for brevity ....
}
These are the logs that appear in the console when it occassionally works:
Client subscribed to hub 'appointmenthub'
Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22appointmenthub%22%7D%5D'
webSockets transport starting.
Connecting to websocket endpoint 'ws://localhost:52541/signalr/connect?....
Websocket opened.
webSockets transport connected. Initiating start request.
The start request succeeded. Transitioning to the connected state.
Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
I wrote 'occassionally' on purpose because every now and then SignalR connects correctly. Sadly however most of the time I don't get that far. Usually the last visible step in the console is :
webSockets transport connected. Initiating start request.
For some time I thought it was the body of the callback method that caused the issue I could connect more when I had an empty function but even that isn't the cause. So I'm running out of ideas what to do next.
For the sake of completeness, here is the startup code in ASP.NET MVC:
GlobalHost.Configuration.TransportConnectTimeout = TimeSpan.FromSeconds(50);
GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);
GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule());
GlobalHost.DependencyResolver.Register(typeof(IJavaScriptMinifier), () => new SignalRMinifier());
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new UserIdProvider());
HubConfiguration hubConfiguration = new HubConfiguration()
{
EnableDetailedErrors = true,
};
return app.MapSignalR(hubConfiguration);
I found a similar issue but no solution has been provided there.
I tested this in IE/Edge/Chrome/Firefox/Opera and all have the same results. The application is running on ASP.NET MVC5 and the latest version of SignalR (2.2.1) is used.
So it turns out there was nothing wrong with the client side code or the configuration of SignalR, but the problem was situated in the Hub class.
I had some custom code in the OnConnected method which was causing timeouts and/or errors (connections to external services and databases were made there). By dispatching the work to a different process (e.g. using Hangfire or NServiceBus) I was able to fix the issue as mentioned before.
In hindsight the described behavior makes total sense. The lesson I learned here is to treat Hubs like controllers in ASP.NET MVC: they should be fat free and should contain limited business logic.
Related
The post might seem a bit lengthy but please bear with me.
I'm trying to figure out for a couple of days now very mysterious behavior of my express/node app.
My stack is:
nodejs/express (with setInterval polling SNMP endpoint)
AWS (medium instance with 8GB EBS)
amazon-linux
https sever running on port 3000 (the whole app is running on it)
pm2 (as a node process manager - tried foreverjs too with the same results)
The server looks like this:
let debug = require('debug')('server'),
app = require('../app');
app.set('port', process.env.PORT || 3000);
process.on('uncaughtException', (exception) => {
debug(`UncaughtException: ${exception}`);
});
process.on('unhandledRejection', (reason) => {
debug(`UnhandledPromiseRejection: ${reason}`);
});
let server = app.listen(app.get('port'), function () {
debug('Express server listening on port ' + server.address().port);
});
The app itself contains two parts, HTTP routes handling API calls and so-called roller which is a class and it looks like this:
class SnmpPoller {
constructor () {
this.snmpAdapter = null;
this.site = config.get('system.site');
}
startPolling () {
debug('Snmp poller started');
timers.setInterval(
this.poll(),
config.get('poll.interval')
);
}
poll () {
return () => {
if (dbConnection.get()) {
debug('Polling data');
this.doMagic(this.site);
}
};
}
// other super useful methods
}
The poller runs a function every poll.interval seconds.
doMagic method calls very complicated mechanism of polling data from different endpoints with a lot of promises and callbacks. It saves data to at least 4 different MongoDB collections, parsing and calculating different values along.
All is good here. The poller is working fine all the promises are handled, all the errors are handled.
I put logs to each and every callback and promise.
Now, the situation is as follows:
When I leave the app running for several hours, it becomes unresponsive. When I try to reach it using postman I'm getting didn’t send any data. ERR_EMPTY_RESPONSE. It is definitely not 404 error. The request knows that there is something but cannot access it.
Also, pm2 is not restarting the app, there is nothing in the log files, so it seems that it's not caused by the app itself.
I was suspecting memory leaks and unhandled promises but I checked it, and all is fine, the garbage collector is behaving properly keeping the app memory at around 40-50Mb. I also got rid of all unhandled promises during the process.
I also ruled out db connection issues. Double checked if it's not happening on app losing the connection with the db. It's not the problem.
The QUESTION:
Why is it happening, I can not find the cause for a couple of days now. I have exactly the same setup running on production and it's not "crushing" there. (production is not an AWS server)
Might it be something specific to AWS, amazon-Linux?
Any help greatly appreciated.
Thanks!
I've a client to server Websocket connection which should be there for 40 seconds or so. Ideally it should be forever open.
The client continually sends data to server and vice-versa.
Right now I'm using this sequence:
var socket;
function senddata(data)
{
if (!socket)
{
socket = new WebSocket(url);
socket.onopen = function (evt) {
socket.send(data);
socket.onmessage = function (evt) {
var obj = JSON.parse(evt.data);
port.postMessage(obj);
}
socket.oneerror = function (evt) {
socket.close();
socket = null;
}
socket.onclose = function(evt){
socket = null;
}
}
}
else
{
socket.send(data);
}
}
Clearly as per current logic, in case of error, the current request data may not be sent at all.
To be frank it sometimes gives error that websocket is still in connecting state. This connection breaks often due to networking issues. In short it does not work perfectly well.
I've read a better design : How to wait for a WebSocket's readyState to change but does not cover all cases I need to handle.
Also I've Googled about this but could not get the correct procedure for this.
So what is the right way to send regular data through Websockets which handles well these issues like connection break etc?
An event you don't seem to cover is onclose. Which should work really well, since it's called whenever the connection terminates. This is more reliable than onerror, because not all connection disruptions result in an error.
I personally use Socket.IO, it enables real-time bidirectional event-based communication between client and server.
It is event driven. Events such as
on connection :: socket.on('conection',callback);
and
on disconnect :: socket.on('disconnect',callback);
are built in with socket.io so it can help you with your connection concerns. Pretty much very easy to use, check out their site if you are interested.
I use two-layer scheme on client: abstract-wrapper + websocket-client:
The responsibilities of the websocket-client are interacting with a server, recovering the connection and providing interfaces (event-emitter and some methods) to abstract-wrapper.
The abstract-wrapper is a high-level layer, which interacts with websocket-client, subscribes to its events and aggregating data, when the connection is temporary failed. The abstract-wrapper can provide to application layer any interface such as Promise, EventEmitter and so on.
On application layer, I just work with abstract-wrapper and don't worry about connection or data losing. Undoubtedly, it's a good idea to have here information about the status of connection and data sending confirmation, because it's useful.
If it is necessary, I can provide some code for example
This apparently is a server issue not a problem in the client.
I don't know how the server looks like here. But this was a huge problem for me in the past when I was working on a websocket based project. The connection would continuously break.
So I created a websocket server in java, and that resolved my problem.
websockets depend on lots of settings, like if you're using servlets then servlet container's settings matter, if you're using some php etc, apache and php settings matter, for example if you create a websocket server in php and php has default time-out of 30 seconds, it will break after 30 seconds. If keep-alive is not set, the connection wont stay alive etc.
What you can do as quick solution is
keep sending pings to a server after a certain amount of time (like 2 or 3 seconds, so that if a websocket is disconnected it is known to the client so it could invoke onclose or ondisconnect, I hope you know that there is no way to find if a connection is broken other than failing to send something.
check server's keep-alive header
If you have access to server, then it's timeouts etc.
I think that would help
I opened a question here earlier (Socket.io trigger events between two node.js apps?), this was much help, but I am confused out of my mind.
I keep getting object is not a function on my client side script.
A little setup, I have a front end site that is served with express localhost:9200 then I have a back end app localhost:3100 that is also served with express and I am trying to emit events from localhost:9200 to the socket.io server localhost:3100
Client script for website localhost:9200
// I have tried many different ways
var socket = io('http://localhost:3100');
var socket = io('http://localhost');
var socket = io();
EDIT
The issue was with the above of course, because io in the above case for some reason was an object when it should be a function, I came across an old post which mentioned using var socket = io.connect('http://localhost:3100'); connect and that worked, I though it was depreciated or something, I have no clue why the docs don't mention this but it fixed my issue.
All result in object is not a function. I include the client side script like this
// tried some different ways
<script src="http://localhost:3100/socket.io/socket.io.js"></script>
<script src="socket.io/socket.io.js"></script> // this is a 404
I have installed https://github.com/automattic/socket.io-client and on the server for the front end website :9200 I have set it up like.
// tried a couple ways to connect
var socket = require('socket.io-client')('http://localhost:3100');
var socket = require('socket.io-client')('http://localhost');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
I am confused on how to properly configure this so that I can get my site to emit socket events to my server and visa versa?
Well I figured it out, this is pretty ridiculous but on the client side javascript I needed to add var socket = io.connect('http://localhost:3100'); the io.connect made it work versus var socket = io('http://localhost:3100');
Maybe I missed it but the docs don't say to use io.connect https://github.com/automattic/socket.io-client whatever it works and I am happy, any thoughts on why the docs don't mention this would be great.
The difference is io.connect is pre 1.0 syntax. They changed it for whatever reason. These are the exact kind of fun surprises I have come to expect in socket.io.
I have a Problem With IE and SignalR, I'm using the it to perform a Syncing action between two databases, the Actions Completed successfully on Google Chrome / Firefox / Safari in all scenarios.
Using IE for the First time the sync performed successfully but only for one time, in the second time a pending request stack and the page stay freeze for ever.
I found a solution online which is changing the transport mode.
But page still freezing.
if (isIE()) {
$.connection.hub.start({ transport: ['serverSentEvents','foreverFrame']}).done(function () {
progressNotifier.server.DoMyLongAction();
});
}else{
$.connection.hub.start({ transport: ['serverSentEvents','longPolling'] }).done(function () {
progressNotifier.server.DoMyLongAction();
});
}
I'm Using:
SgnalR v2.1.0.0
.Net framework v4.5
jquery v1.8
is it an Issue or I'm Doing something wrong ?
Edit
my application use Jquery progress bar and i Update this progress bar using this Code:
server side:
Clients.Caller.sendMessage(msg, 5, "Accounts");
client side:
progressNotifier.client.sendMessage = function (message, value, Entity) {
pbar1.progressbar("value", nvalue);
};
it's working on Firefox so I thought it's a signalR Issue !! Now i became confused if it's working as expected then what causes this problem ?
you can try use EventSource (SSE).
I am using this:
https://github.com/remy/polyfills/blob/master/EventSource.js
but modified, for SignalR:
http://a7.org/scripts/jquery/eventsource_edited.js
I am working with it for one year, SignalR just check for window.EventSource and it works.
The solution you found online is not likely to help your issue.
I doubt your IsIE() function is correctly identifying IE. If it was, SignalR should only be attempting to establish a "foreverFrame" connection, since IE does not even support "serverSentEvents". I would not expect IE to make any "/signalr/poll" requests, because those requests are only made by the "longPolling" transport.
Also, having a "pending" poll request in the IE F12 tool's network tab is entirely expected. This is how long polling is designed to work. Basically, as soon as a message is received the client makes a new ajax request (a long poll) to retrieve new messages. If no new messages are immediately available, the server will wait (for up to 110 seconds by default in the case of SignalR, not forever) for a new message to be sent to the client before responding to the pending long poll request with the new message.
Can you clarify exactly what issue you are having other than seeing a pending poll request showing up under the network tab? It would also help if you you enabled tracing in the JS client, provided the console output, and showed all the "/signalr/..." requests in the network tab.
I'm building a simple real-time chat app to learn how to use websockets with RoR and I don't think I'm understanding how channels work because they're not doing what I expect. I can successfully send a message to my Rails app using the dispatcher.trigger() method, and use my websocket controller to broadcast a message to all clients that subscribe to the channel. That all works fine. What does NOT work is using a channel (via the channel.trigger() method) to send a message to other clients. The websocket-rails wiki says...
Channel events currently happen outside of the Event Router flow. They
are meant for broadcasting events to a group of connected clients
simultaneously. If you wish to handle events with actions on the
server, trigger the event on the main dispatcher and specify which
controller action should handle it using the Event Router.
If I understand this correctly, I should be able to user the channel.trigger() method to broadcast a message to clients connected to the channel, without the message being routed through my RoR app, but it should still reach the other connected clients. So here's my code...
var dispatcher = new WebSocketRails('localhost:3000/websocket');
var channel = dispatcher.subscribe('channel_name');
channel.bind('channel_message', function(data) {
alert(data.message);
});
$("#send_message_button").click(function() {
obj = {message: "test"};
channel.trigger('channel_message', obj);
});
With the code listed above, I would expect that when I click the button, it sends a channel message using channel.trigger() and the channel_message binding should be executed on all clients, displaying an alert that reads "test". That doesn't happen. I'm using Chrome tools to inspect the websocket traffic and it shows the message being sent...
["channel_message",{"id":113458,"channel":'channel_name',"data":{"message":"test"},"token":"96fd4f51-6321-4309-941f-38110635f86f"}]
...but no message is received. My questions are...
Am I misunderstanding how channel-based websockets work with the websocket-rails gem?
If not, what am I doing wrong?
Thanks in advance for all your wisdom!
I was able to reproduce a working copy based on an off-the-shelf solution from the wiki along with your very own code.
I've packaged the whole thing here. The files you might be interested are home_controller.rb, application.js and home/index.html.erb.
It seems your understanding of channel-based websockets is correct. About the code, make sure to load the websocket javascript files and to enclose your code inside a document.ready. I had the exact same problem you're having without the latter.
//= require websocket_rails/main
$(function() {
// your code here...
});
Let me know if it works. Best Luck!