Continually reading WebSockets via Webmachine

60 views
Skip to first unread message

James Le Cuirot

unread,
Mar 25, 2016, 5:33:03 AM3/25/16
to Celluloid
Hello,

This is kinda two questions in one.

First of all, how can you detach a WebSocket connection when using Webmachine? There are no examples of this and I suspect it isn't possible because the websocket_handler callback only passes the WebSocket. There is no way to get the associated connection from it. I could file a pull request to add it.

Second, what's the best way to continually read from the WebSocket? The roundtrip.rb example appears to poll the socket every second, which isn't great. The read call blocks so it's not all that bad but still a strange way to do it. I tried this.

Webmachine::Application.new do |app|
  app
.configure do |config|
    config
.port = 8080
    config
.adapter = :Reel

    config
.adapter_options[:websocket_handler] = proc do |websocket|
     
SocketHandler.new(websocket)
   
end
 
end
end

class SocketHandler
  include
Celluloid

 
def initialize(websocket)
   
@socket = websocket
    async
.run
 
end

 
def run
    loop
do
      data
= @socket.read
     
# Do stuff.
   
end
 
rescue IOError
 
end
end

It does work but it still doesn't feel right, especially after I saw the on_message method. I then tried this but it never fires. What am I doing wrong?

class SocketHandler
  include
Celluloid

 
def initialize(websocket)
   
@socket = websocket
   
@socket.on_message { |data| receive data }
 
end

 
def receive(data)
   
# Do stuff.
 
end
end

James Le Cuirot

unread,
Mar 28, 2016, 6:01:09 PM3/28/16
to Celluloid


On Friday, 25 March 2016 09:33:03 UTC, James Le Cuirot wrote:

class SocketHandler
  include
Celluloid

 
def initialize(websocket)
   
@socket = websocket
    async
.run
 
end

 
def run
    loop
do
      data
= @socket.read
     
# Do stuff.
   
end
 
rescue IOError
 
end
end

It does work but it still doesn't feel right, especially after I saw the on_message method.

Well I think I've found the first problem with this approach. It prevents a notification subscription from firing. I'm kinda stuck now. :( 

Виталий Трофимов

unread,
Jun 20, 2017, 4:53:01 PM6/20/17
to Celluloid
Greping for 'on_message' gives you a one tip on using that function in ./spec/reel/websocket_spec.rb


class MyActor
  include
Celluloid
 
def initialize(websocket)
    websocket
.read_every 0.1
 
end
end  
f
= Celluloid::Future.new
websocket.on_message do |message|
  f << Celluloid::SuccessResponse.new(:on_message, message)
end



вторник, 29 марта 2016 г., 1:01:09 UTC+3 пользователь James Le Cuirot написал:


It does work but it still doesn't feel right

Web should be felt that way in every aspect of it.
Reply all
Reply to author
Forward
0 new messages