EzDevInfo.com

websocket-rails

Plug and play websocket support for ruby on rails.

Redis error on websocket-rails when sending an event to a specific client

I am using 'threadsocket-rails'branch of websocket-rails gem and RedisCloud service of heroku with memory size 30MB.(link: https://elements.heroku.com/addons/rediscloud)

In my config, I have enable channel synchronization and set redis_options as:

config.redis_options = { :host => APP_CONFIG['REDIS_CLOUD_HOST'],
                            :port => APP_CONFIG['REDIS_CLOUD_PORT'],
                            :password => APP_CONFIG['REDIS_CLOUD_PASSWORD']}

When android or ios devices trigger the event, they get this empty hash (when I use redis):

["add_to_channel",{},{"id":null,"channel":null,"user_id":null,"success":false,"result":null,"token":null,"server_token":null}]

I also get this error:

 NameError Exception: undefined local variable or method `sync' for #<WebsocketRails::UserManager::LocalConnection:0x007fe271873c18>

Otherwise they get correct response("message":"user join new channel"):

["add_to_channel",{"message":"user join new channel"},{"id":null,"channel":null,"user_id":null,"success":false,"result":null,"token":null,"server_token":null}]

I use this code to send an event to a specific client:

WebsocketRails.users[recipient_id].send_message('add_to_channel', {:message => message[:body]})

Source: (StackOverflow)

Websockets with Rails(Puma) - Error during WebSocket handshake: Unexpected response code: 200

I am trying to use websocket in my Rails4.1 application

Here are some relevant code snippets:

Gemfile:

gem 'websocket-rails'
gem 'puma'

development.rb

 config.middleware.delete Rack::Lock

I am starting the server locally as:

bundle exec puma -p 3000

In the chrome console I see a connection error:

new WebSocketRails('localhost:3000/post/hello', true);

WebSocket connection to 'ws://localhost:3000/post/hello' failed: Error during WebSocket handshake: Unexpected response code: 200

Can anyone help with what I need to do to use web sockets locally in Rails?


Update1

I tried adding following as per Websockets not working in my Rails app when I run on Unicorn server, but works on a Thin server but it did not help

initializers/eventmachine.rb

Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive

Source: (StackOverflow)

Advertisements

gem "websocket-rails" and its scalability

we are currently developing a chat (like facebook, with stored messages). at the moment, theres a minimum of 500 online users (its a dating website) and at the peak there is a max of 3000 users simultanously online.

switching to websockets is "the thing" for us, but while using the gem "websocket-rails" we fear a little the performance.reading articles like https://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/ is causing some doubts.

so our question is:

does websocket-rails is killing our application or not? the other choice would be running a jsnode server and switch to faye which shouldnt be a problem in our scalabitliy. does somebody is having any expereince with the scalability of websocket-rails?


Source: (StackOverflow)

Websocket-rails not triggering message to channel (standalone, synchronized)

I'm trying to use websocket-rails (standalone, synchronized w/ redis on local.host:3245) with my Rails Grape API (running on local.host:3001)

Now I try to send messages to channel A from one of my Grape API endpoints using for example:

WebsocketRails[:a_channel].trigger('welcome_message', 'Hello world!')

Then in my rails client (running on local.host:3005) I try to obtain the message and show it in the browser. Using the WebsocketRails js client I am able to connect with the server. Subscribe to the channel and bind to the event suggested in the wiki:

var dispatcher = new WebSocketRails('local.host:3245/websocket');
var channel = dispatcher.subscribe('a_channel');
channel.bind('welcome_message', function(data) {
  console.log('channel event received: ' + data);
});

However nothing shows up in my console.log, but when I look at the websocket_rails.log I see:

[2015-08-18 10:03:23.184] [[32mChannel[0m] [a_channel] {"welcome_message"=>"Hello world!"}

When I look at redis using redis-cli monitor I don't see the websocket_rails keys (at least I assume they should be there...) and also in my

chrome developer environment --> websockets --> headers I see the ping / pong between client and server, but all the variables are empty.

I do not make use of the WebsocketControllers or events.rb routes, should I? based on this wiki page I assumed I didn't have to.. Also I rather not make use of the controllers and just keep everything in my Grape files because of API versioning.

Does anyone have a suggestion / ideas how to fix this problem?

Thanks in advance!

Note:

  • Rails 4.2.3
  • Ruby 2.1.0
  • API server is running on Unicorn
  • rails client is running on Thin

Source: (StackOverflow)

Calling websocket-rails from a ruby client

I have a pure ruby client where I want to send a message over a websocket connection. The websocket runs inside a rails application and I am using the websocket-rails gem ( https://github.com/websocket-rails/websocket-rails )

With the following code I am able to connect to my websocket successfully but I can not receive any messages.

require 'rubygems'
require 'websocket-client-simple'

ws = WebSocket::Client::Simple.connect 'ws://localhost:3000/websocket'

ws.on :message do |msg|
  puts msg.data
end

ws.on :open do
  ws.send 'hello!!!'
end

ws.on :close do |e|
  p e
  exit 1
end

ws.on :error do |e|
  p e
end

loop do
  ws.send "foo"
end

This is probably because I cannot specify an event name for the message as is expected for websocket-rails communication. Is there somehow a way to communicate with a rails (or python) client with the websocket-rails websocket?


Source: (StackOverflow)

How to disable client side publishing to a channel in websocket-rails?

I would like to use websocket-rails gem for my application where websockets with publish / subscribe is required. But it provides this feature where javascript client can publish to the channel. I want to only allow server side event publish and don't want to allow anybody to just publish what they want since it could mess with other users experience. I don't see any possibility to disable this feature in project documentation.

https://github.com/websocket-rails/websocket-rails/wiki/Working-with-Channels


Source: (StackOverflow)

Trigger/Subscribe to websocket-rails event from inside a rails runner

I have a Rails Application with websocket-rails gem.

Inside my application there is a Daemon that I launch with rails runner MyDaemon.start

I'm using websocket-rails Synchronization, so my config/initializers/websocket_rails.rb looks like this:

WebsocketRails.setup do |config|
  config.log_internal_events = false
  config.standalone = false
  config.synchronize = true
end

Inside MyDaemon, using synchronization, I can trigger event that will reach both my WebsocketRails::BaseController and my javascript WebSocketRails.

What I'm trying to do is to find a way to bind to events from my MyDaemon.

I've tried to implement a plain WebSocket client using both faye-websocket-ruby and websocket-client-simple, but after banging my head on my keyboard for some time, I figured out that there is some kind of "handshake" process using connection_id from the client_connected message. Basically none of the solutions provided in this other so question works for me.

I need to understand if inside my MyDaemon I can subscribe directly to some WebsocketRails callback, even inside an EventMachine, or how should I implement a Websocket Client in Ruby itself.

My last attempt to have a ruby client can be found in this gist, and this is a sample output:

ruby client.rb ws://localhost:3000/websocket
[:open, {"upgrade"=>"websocket", "connection"=>"Upgrade", "sec-websocket-accept"=>"zNTdGvxFKJeP+1PyGf27T4x2PGo="}]

JSON message is
[["client_connected", {"id"=>nil, "channel"=>nil, "user_id"=>nil, "data"=>{"connection_id"=>"4b7b91001befb160d17b"}, "success"=>nil, "result"=>nil, "token"=>nil, "server_token"=>nil}]]

client id is 4b7b91001befb160d17b
[:message, "[[\"client_connected\",{\"id\":null,\"channel\":null,\"user_id\":null,\"data\":{\"connection_id\":\"4b7b91001befb160d17b\"},\"success\":null,\"result\":null,\"token\":null,\"server_token\":null}]]"]

JSON message is
[["websocket_rails.ping", {"id"=>nil, "channel"=>nil, "user_id"=>nil, "data"=>{}, "success"=>nil, "result"=>nil, "token"=>nil, "server_token"=>nil}]]

Sending ["pong",{}]
[:message, "[[\"websocket_rails.ping\",{\"id\":null,\"channel\":null,\"user_id\":null,\"data\":{},\"success\":null,\"result\":null,\"token\":null,\"server_token\":null}]]"]
[:close, 1006, ""]

While the log of websocket-rails is:

I [2015-06-27 02:08:45.250] [ConnectionManager] Connection opened: #<Connection::2b3dddaf3ec4ed5e3550>
I [2015-06-27 02:08:45.251] [Dispatcher] Started Event: client_connected
I [2015-06-27 02:08:45.251] [Dispatcher] Name: client_connected
I [2015-06-27 02:08:45.251] [Dispatcher] Data: {"connection_id"=>"2b3dddaf3ec4ed5e3550"}
I [2015-06-27 02:08:45.251] [Dispatcher] Connection: #<Connection::2b3dddaf3ec4ed5e3550>
I [2015-06-27 02:08:45.251] [Dispatcher] Event client_connected Finished in 0.000174623 seconds
I [2015-06-27 02:09:05.252] [ConnectionManager] Connection closed: #<Connection::2b3dddaf3ec4ed5e3550>
I [2015-06-27 02:09:05.252] [Dispatcher] Started Event: client_disconnected
I [2015-06-27 02:09:05.252] [Dispatcher] Name: client_disconnected
I [2015-06-27 02:09:05.252] [Dispatcher] Connection: #<Connection::2b3dddaf3ec4ed5e3550>
I [2015-06-27 02:09:05.253] [Dispatcher] Event client_disconnected Finished in 0.000236669 seconds

Probably I'm missing somethig very stupid, so I'm here to ask your help!


Source: (StackOverflow)