Since v2.4.0, you can enable an 'Inline Client' to allow the server to act as a client itself. This allows the server to directly publish, subscribe, and unsubscribe to topics, bypassing standard ACL and topic validation checks (allowing access even to $SYS topics).
Enable Inline Client
Enable the feature in the mqtt.Options during server initialization:
server := mqtt.New(&mqtt.Options{
InlineClient: true,
})
Inline Publish
Use server.Publish to send a message directly from the server:
err := server.Publish("direct/publish", []byte("packet scheduled message"), false, 0)
Note: The QoS level only applies to the subscribers, following the MQTT v5 specification.
Inline Subscribe
Use server.Subscribe with a callback function to handle incoming messages. The default QoS for inline subscriptions is 0.
callbackFn := func(cl *mqtt.Client, sub packets.Subscription, pk packets.Packet) {
server.Log.Info("inline client received message from subscription", "client", cl.ID, "subscriptionId", sub.Identifier, "topic", pk.TopicName, "payload", string(pk.Payload))
}
server.Subscribe("direct/#", 1, callbackFn)
Inline Unsubscribe
To stop receiving messages from an inline subscription:
server.Unsubscribe("direct/#", 1)