RPC middlewares
AnyCable Ruby allows to add custom middlewares to the RPC server (both standalone gRPC and embedded HTTP versions).
For example, anycable-rails
ships with the middleware that integrate Rails Executor into RPC server.
Exceptions handling is also implemented via AnyCable middleware.
Adding a custom middleware
AnyCable middleware is a class inherited from AnyCable::Middleware
and implementing #call
method:
class PrintMiddleware < AnyCable::Middleware
# request - is a request payload (incoming message)
# handler - is a method (Symbol) of RPC handler which is called
# meta - is a metadata (Hash) provided along with request
def call(handler, request, meta)
p request
yield
end
end
NOTE: you MUST yield the execution to continue calling middlewares and the RPC handler itself.
Activate your middleware by adding it to the middleware chain:
# anywhere in your app before AnyCable server starts
AnyCable.middleware.use(PrintMiddleware)
# or using instance
AnyCable.middleware.use(ParameterizedMiddleware.new(params))