CoupDeMistral
CoupDeMistral

Reputation: 198

sending messages to a websocket server using em-websocket-client

I am new to websockets and am trying to send messages to an existing websocket server that expects a subscription request. I tried:

require 'em-websocket-client'

EM.run do
  ws = EventMachine::WebSocketClient.connect("ws://localhost:3000")
  ws.send_msg "this is a test message"
end

but I get

'undefined method `version' for nil:NilClass'. 

I can read streaming data correctly from the server by replacing the send_mess line with

  ws.stream do |msg|
    puts "received msg <#{msg}>"
  end

So at least I know that I am connecting correctly to the server. What am I doing wrong?

Upvotes: 2

Views: 329

Answers (1)

Jason
Jason

Reputation: 2733

Apparently you have to put the call to send_msg inside a WebSocketClient callback function, like this:

require 'em-websocket-client'

EM.run do
  ws = EventMachine::WebSocketClient.connect("ws://localhost:3000")
  ws.callback do
      ws.send_msg "this is a test message"
  end
end

See this for more (but, unfortunately, not enough) information.

Upvotes: 2

Related Questions