connection lost while using sinatra-websocket gem in ruby - ruby

I want to use websocket in sinatra in my social game and first i wanted to use em-websockets but then i found a good gem for websockets and it use em-websockets .. it's called sinatra-websocket here ..
https://github.com/simulacre/sinatra-websocket
and i have a problem that i found also in the example of the gem here
https://github.com/simulacre/sinatra-websocket/blob/master/examples/echochat.rb
the problem is if any of the clients are idle for while (not sending or receiving) .. all the connections got an error on the browser (the connection is interrupted while loading) ..and then no client can send anything to the server by websocket !!!
anyone knows what's happening ?? thanks a lot !!

Related

how to terminate inactive websocket connection in passenger

Past few days we are struggling with inactive websocket connections. The problem may lay on network level. I would like to ask if there is any switch/configuration option to set timeout for websocket connection for Pushion Passenger in standalone mode.
You should probably solve this at the application level, because solving it in other layers will be more ugly (less knowledge about websocket).
With Passenger Standalone you could try to set max_requests. This should cause application processes to be restarted semi-regularly, and when shutting down a process Passenger should normally abort websocket connections.
If you want more control over the restart period you could also use for example a cron job that executes rolling restarts every so often, which shouldn't be noticeable to users either.
Websockets in Ruby and Passenger (and maybe node.js as well) aren't "native" to the server. Instead, your application "hijacks" the socket and controls all the details (timeouts, parsing etc').
This means that a solution must be implemented in the application layer (or whatever framework you're using), since Passenger doesn't keep any information about the socket any more.
I know this isn't the answer you wanted, but it's the fact of the matter.
Some approaches use native websockets where the server controls websocket connections (timeouts, parsing etc', i.e. the Ruby MRI iodine server), but mostly websockets are "hijacked" from the server and the application takes full control and ownership of the connections.

Unable to use wscat to connect to an ActionCable WebSocket on Ruby on Rails 5 Beta

I ran the ActionCable chat room sample that ships with Rails 5 Beta. The web app ran fine with data flowing up and down the WebSocket.
However, when I tried to connect to the WebSocket directly using wscat from the command line I got a connection error.
The wscat connection attempt:
wscat -c ws://localhost:3000/cable
The error response:
error: Error: connect ECONNREFUSED
At first, I assumed Rails forgery protection prohibited the connection. So I added the following to config/environments/development.rb:
config.action_cable.disable_request_forgery_protection = true
However, I experienced the same connection error.
I tried using the wsd command line util to connect to the ActionCable WebSocket. wsd was worse, it crashed with a kernel panic.
I also tested both wscat and wsd with other WebSockets, including the WebSocket Echo Test http://www.websocket.org/echo.html, and they worked fine.
I'm stumped. Any suggestions?
1) Spin up ActionCable via standalone server. (this step needs verifying, but since ActionCable docs are still murky at the moment I will just layout the steps I did to make this work).
Running bin/cable on rails 5.0.0beta1 should fire up puma on port 28080.
(protip: make sure you don't have other conflicting puma processes running on config/puma.rb as puma will default load. You'll remember this and thank me later when you try to deploy rails app)
2) ActionCable::Connection::Base handles all incoming connections and decides how to identify users (e.g. current_user) or to reject unauthorized connections.
From the actioncable examples (https://github.com/rails/actioncable-examples) this is defined by cookie in /app/channels/application_cable/connection.rb.
Websocket connections from wscat will be rejected without pass authentication. You'll need to tweak this to fit your needs.
ActionCable still has alot of other "surprises". I hope to make update this post and/or make blog post documenting my experiences getting an iOS client to talk to ActionCable.
Good luck!

Making request using WebSockets in sails but not receiving response from the server

I'm starting with Websockets and I have a problem.
I have a sails.js application that uses sockets to update the client side.
On the client side it makes an API call using socket.get("/api/v1/actor...") to bring all the items of the database. When I see what the WebSocket's traffic on the Chrome console:
As you can see, the connection has been established and the API call has been correctly done through the socket.
The problem is, there is no answer from the server, not even an error.
If I make the same API call using ajax, I get response, but it doesn't work using WebSockets.
Any idea what might be producing this behavior?
EDIT: I add here the code here that processes the request and this one here that sends the request, but the problem is that it never execute this code. I think we we are closer to the find the cause, since we think it has to do with a network problem. We figured there is an F5 reverse-proxy which is not properly set up to handle websockets
The answer didn't make any sense now that I've seen the code that's why I've edited it. I only answered because I could't comment on your question and ask you for the code.
Your calling code seems correct and the server side of things the process of response should be handled automatically by the framework, you only need to return some JSON in the controller method.
I instantiated a copy of the server (just changed the adapters to run it locally) and the server replied to the web socket requests (although I only tested the route '/index').
Normally when the problems are caused by a reverse proxy the socket simply refuses to connect and you can't even send data to server. Does the property "socket.socket.connected" returns true?
The best way to test is to write a small node application with socket.io client and test it in the same machine that the application server is running, then you can exclude network problems.

Browser not sending any data (no errors)

So I have a faye server and a rails web application. It all worked fine till recently. Now the browser is not sending anythin to the server. There a no error not on the server not even in browser (I can create an instance of Faye.Client just fine). But when I do publish the server gets nothing. It doesn't even get anythin on the meta channel, like connect or subscribe. The thing is that if I send something with curl the server shows the message (cause I'm logging it).
I can't pin point the problem. :/
Make sure the code to send messages is being reached. Make sure your client is still valid. Test an earlier code version etc. Just a few ideas.
Solved the problem. The url to which faye was connecting was not OK, but faye failed silently because the url responded. :/

Continuous websocket traffic with SignalR

I've been using SignalR successfully for some time using serverSentEvents transport. I've just installed my application on Windows Server 2012 RC and now when I connect SignalR is using websockets (as you'd expect).
It appears to work OK except there is a continuous stream of websocket traffic between server and browser - about once a second it's sending about 90 bytes.
From the debugger window in Chrome, it looks like it's sending a connection upgrade request each time.
I am successfully receiving messages sent by the server, but I seem to be worse off than when using long polling.
The browser is Chrome version 19.0.1084.52.
Any idea why this is happening?
This was down to my own stupidity - but I thought I'd answer in case anyone else has the same problem.
I had built the solution SignalR.Hosting.AspNet instead of SignalR.Hosting.AspNet45
So the method AcceptWebSocketRequest was throwing a NotSupportedException; because the client websocket upgrade failed, it just tried again about a second later.
I guess the client could show an error or something in this case to aid debugging.

Resources