https://github.com/recruitee/mq_tools
Easily define RabbitMq rpc providers
https://github.com/recruitee/mq_tools
Last synced: 8 months ago
JSON representation
Easily define RabbitMq rpc providers
- Host: GitHub
- URL: https://github.com/recruitee/mq_tools
- Owner: Recruitee
- Created: 2017-11-07T14:54:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-05-15T13:40:08.000Z (about 6 years ago)
- Last Synced: 2024-12-27T01:11:06.821Z (over 1 year ago)
- Language: Elixir
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 28
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MQTools
Easily defined rabbitmq providers and client for "rpc" pattern - as described here: https://www.rabbitmq.com/tutorials/tutorial-six-elixir.html
This software is still in early beta version. Use at your own discretion.
## MQTools.Provider
Let's you define rabbitmq 'rpc endpoints'.
* Add `mq_tools` to dependencies in mix.exs
* Configure the module:
```
config :mq_tools, :mq_providers,
connection: [
host: "localhost",
port: 5672,
virtual_host: "/",
user: "guest",
password: "guest"
]
```
* Write a module defining RPC handlers:
```
defmodule MyRpcHandlers do
use MQTools.Provider
defrpc "foo.bar" do
%{"something" => something} -> "reply..."
_ -> "handle other payload"
end
end
```
and add the module to the config:
```
config :mq_tools, :mq_providers,
rpc_providers: [MyRpcHandlers],
```
## MQTools.Client
Call previously defined providers. The one defined above could be called like so:
```
> MQTools.Client.call("foo.bar", %{something: "here"})
=> "reply..."
> MQTools.Client.call("foo.bar", %{different: "request"})
=> "handle other payload"
```
In case you just want to publish a message and you are not interested in the reply you can do the following:
```
> MQTools.Client.publish("foo.bar", %{something: "here"})
=> :ok
```
There is also optional client timeout param if you ever need it (default 10000ms):
```
> MQTools.Client.call("foo.bar", %{something: "here"}, 7500) # timeout in ms
=> "slow reply..."
```
## Optional message encoding configuration
By default the messages are transported using json. If you want to change that you can define your own message encoder/decoder module.
```
defmodule MyOwnMsgPacker do
@behaviour MQTools.Packer
def pack(term) do
SuperPackLib.pack(term)
end
def unpack(string) do
SuperPackLib.unpack(string)
end
end
```
and set it in the config as your packer module:
```
config :mq_provider,
packer: MyOwnMsgPacker
````
## Kudos
Orignally written by https://github.com/nilclass
## License
MQTools is released under the [MIT License](https://opensource.org/licenses/MIT).