View Source PintBroker (pint_broker v1.0.0)
A simple, pint-sized MQTT broker that can be used for testing and development
Warning
This is not indended for production use and makes no attempts for large connection scaling and handling. It is also not considered feature complete, but handles most simple use cases.
Supported:
- Simple, unencrypted TCP connections
- MQTT v3.1.1
- QoS 0
- Connect, publish, subscribe, and unsubscribe
- Ping requests
- Rule forwarding (see below)
Unsupported:
- SSL connections
- QoS 1 and 2
- MQTT v5
You can specify custom :gen_tcp
options with :overrides
key when starting
the server.
Rule forwarding
Many production setups will have a few topics with rules that forward
messages to a handler such as an SQS queue or central service. This allows
for scaling of many devices to communicate with a few nodes. For testing,
you can specify rules when starting the broker which will forward
Tortoise311.Package.Publish
structs to a handler function or process in
order to mimic this rule forwarding behavior. See add_rule/3
for more
information.
Example:
iex> s = self()
iex> handler1 = fn pub -> send(s, pub) end
iex> PintBroker.start_link(rules: [{"my/+/test/topic", handler1}])
{:ok, #PID<0.226.0>}
# You can publish from the broker or another client
iex> PintBroker.publish("my/first/test/topic", "hello world")
iex> flush()
%Tortoise311.Package.Publish{
__META__: %Tortoise311.Package.Meta{opcode: 3, flags: 0},
identifier: nil,
topic: "my/first/test/topic",
payload: "hello world",
qos: 0,
dup: false,
retain: false
}
Summary
Functions
Add a topic filter rule with handler
Returns a specification to start this module under a supervisor.
Publish a message to a topic.
Types
@type opt() :: {:port, :inet.port_number()} | {:name, GenServer.name()} | {:overrides, :gen_tcp.option()} | {:rules, [rule()]}
@type rule() :: {Tortoise311.topic_filter(), pid() | (Tortoise311.Package.Publish.t() -> any()) | (Tortoise311.topic(), Tortoise311.payload() -> any())}
Functions
@spec add_rule(GenServer.server(), Tortoise311.topic_filter(), pid() | function()) :: :ok
Add a topic filter rule with handler
In many broker implementations, there are a few routing rules that forward messages from specific topics to a handler such as an SQS queue or central service in a fan-in pattern for scaling many remote devices to a few nodes.
Many times the topic runs through a full SQL query before forwarding the message on. However, that is unsupported in this simple server. Instead you can provide a simple topic filter and a handler function or process which will receive the Tortoise311.Package.Publish struct that can be split apart by your logic to formulate into the end result needed (such as mimicing an SQS queue message in your producer)
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec publish(GenServer.server(), Tortoise311.topic(), Tortoise311.payload()) :: :ok
Publish a message to a topic.