6

I'm trying to connect to remote websocket using Celluloid and Websocket client based on celluloid (gem 'celluloid-websocket-client'). The main advantage of this client for me is that I can use callbacks in the form of class methods instead of blocks.

require 'celluloid/websocket/client'
class WSConnection
  include Celluloid

  def initialize(url)
    @ws_client = Celluloid::WebSocket::Client.new url, Celluloid::Actor.current
  end

  # When WebSocket is opened, register callbacks
  def on_open
    puts "Websocket connection opened"
  end

  # When raw WebSocket message is received
  def on_message(msg)
    puts "Received message: #{msg}"
  end

  # When WebSocket is closed
  def on_close(code, reason)
    puts "WebSocket connection closed: #{code.inspect}, #{reason.inspect}"
  end

end

m = WSConnection.new('wss://foo.bar')

while true; sleep; end

The expected output is

"Websocket connection opened"

However, I don't get any output at all. What could be the problem?

I am using

gem 'celluloid-websocket-client', '0.0.2'
rails 4.2.1
ruby 2.1.3
Ilya Cherevkov
  • 1,743
  • 2
  • 17
  • 47
  • Have you tried connecting tot the websocket outside of your app? For instance from within browser console – andHapp May 22 '15 at 07:27
  • @andHapp I tried both browser and traditional clients inside my app (like em-websocket) and it worked well – Ilya Cherevkov May 22 '15 at 08:04
  • Actually, I found out, that close callback is working, but on_open and on_message are not – Ilya Cherevkov May 22 '15 at 10:45
  • I've not had enough time lately to give the complete answer, but I want you to know I'm coming back to resolve this for you. – digitalextremist Jun 13 '15 at 09:46
  • @digitalextremist thank you, I resolved it. The problem was with celluloid-websocket-client itself. It does not handle SSL Sockets correctly, I ve pushed pull commit https://github.com/jeremyd/celluloid-websocket-client/pull/15 – Ilya Cherevkov Jun 23 '15 at 13:20
  • I ran into this too. I came to answer with the SSL issue, and saw you noticed it. Do you mind answering your own question and accepting your answer? I watch the `celluloid` tag for unanswered questions and this is at the top right now :) Actually, I will submit an answer with a patch and also instructions on how to react to this in the future, once the library changes. – digitalextremist Jul 15 '15 at 00:49
  • Posted a complete answer. – digitalextremist Jul 15 '15 at 01:04

1 Answers1

5

As you noticed in the comments, the gem had no SSL support. That is the trouble. To expound on the answer, here is a resolution, and also some next steps of what to expect for the future:


[ now ] Override methods in Celluloid::WebSocket::Client::Connection

This is an example injection to provide SSL support to the current gem. Mine is actually highly modified, but this shows you the basic solution:

def initialize(url, handler=nil)
  @url = url
  @handler = handler || Celluloid::Actor.current
  #de If you want an auto-start:
  start
end

def start
  uri = URI.parse(@url)
  port = uri.port || (uri.scheme == "ws" ? 80 : 443)
  @socket.close rescue nil
  @socket = Celluloid::IO::TCPSocket.new(uri.host, port)
  @socket = Celluloid::IO::SSLSocket.new(@socket) if port == 443
  @socket.connect
  @client = ::WebSocket::Driver.client(self)
  async.run
end

The above sends ripple effects through the other methods however, for example, @handler is used to hold the calling actor, which also has the emitter methods on it. Like I said, my version is very different from the stock gem because I got fed up with it and reworked mine. But then:


[ soon ] Use Reel::IO::Client and avoid near certain brain damage.

There are exciting things going on with WebSocket support, and a gem is coming to refactor both server and client implementations of websockets. No more monkeypatches required!

All websocket functionality is being extracted from Reel and being combined with a websocket-driver abstraction, as Reel::IO... in both ::Server and ::Client varieties.

Interestingly, this is prompted by Rails which is moving away from EventMachine to Celluloid::IO for websockets:

A prealpha is online for preview: https://github.com/celluloid/reel-io

digitalextremist
  • 5,952
  • 3
  • 43
  • 62