Handlers - ericchapman/ruby_wamp_worker GitHub Wiki
Registrations and subscriptions are implemented using the Wamp::Worker::Handler module. An example handler that includes both registrations and subscriptions is shown below
class MyHandler
include Wamp::Worker::Handler
register "com.example.add", :add
register "com.example.subtract", :subtract
subscribe "com.example.listener", :listener
def add
args[0] + args[1]
end
def subtract
args[0] - args[1]
end
def listener
# Do something
end
end
Each handler method, in this case add, subtract, and listener, have access to the following attributes
- args [Array] - The arguments that were included with the WAMP request
- kwargs [Hash] - The key-word arguments that were included with the WAMP request
- details [Hash] - The details that were included with the WAMP request
- session [Wamp::Client::Session (or Proxy::Requestor in background handlers)] - A requestor or a session that exposes "call" and "publish" operations to the handler. Note: Be VERY careful making a "call" or "publish" from a handler. If the request gets routed back to the same worker, you may create a deadlock situation where the current handler is waiting for the response and the new handler can't execute until the current handler finishes
The handler methods themselves are instantiated by either calling the register and subscribe methods as shown above OR they can also be done in the configure method. For rules on how to throw errors and what to return from handler methods, see wamp_client.
The register and subscribe methods have the following function prototypes
def subscribe(topic, method, name: nil, **options)
def register(procedure, method, name: nil, **options)
The register and subscribe methods also support specifying the name of the worker explicitly. An example of this is shown below
class MyHandler
include Wamp::Worker::Handler
register "com.example.add", :add, name: :worker1
register "com.example.add", :add, name: :worker2
# Methods...
end
If using Rails, you should place your handlers in app/config/handlers/*
.
Background Handlers
If your application supports Sidekiq, your handlers can be pushed to the Sidekiq worker(s) so that new requests can be processed in parallel. To do this, simply include BackgroundHandler instead of the normal Handler in your handler class as shown below
class MyHandler
include Wamp::Worker::BackgroundHandler
# ...
end
Progress
Registered procedures executing in handlers have access to the "progress" method where a progress value can be returned to those callers who have receive_progress set on their request. This is shown below
class MyHandler
include Wamp::Worker::Handler
register "com.example.add", :add
def add
progress(0)
progress(0.5)
progress(1.0)
args[0] + args[1]
end
end
# The caller in some other piece of code somewhere else...
session.call "com.example.add", [3,4], {}, { :receive_progress => true } do |result, error, details|
puts "#{result[:args]}, #{details[:progress]}"
end
The caller would see the following
[0], true
[0.5], true
[1.0], true
[7], false