Closed garciatruan closed 2 years ago
No, this library currently does not offer a broadcast driver. I don't think it would be very hard to implement, although a few questions might come up very quickly during development:
myapp/channel/{channel}
where myapp/channel
is configurable, but there are other options as well.pusher
and redis
broadcast drivers?I'm open to pull requests for this feature, if you want to give it a try. :+1:
By the way, what is your use case for this? The Laravel broadcasting system is intended to be used for real-time notifications towards users. This normally requires another service which provides a WebSocket connection to the user. Are you planning on writing such service youself?
If you are not planning on broadcasting events to users but to another system, you might be better off with a simple event listener that publishes the data for specific events. To achieve this, you can create and implement a special interface (e.g. ShouldBroadcastViaMqtt
) for events, create an event listener for this interface and publish the events in the listener:
interface ShouldBroadcastViaMqtt
{
public function data(): array;
}
class MessageSent implements ShouldBroadcastViaMqtt
{
public function __construct(
private string $sender,
private string $recipient,
private string $message
) {
}
public function data(): array
{
return [
'sender' => $this->sender,
'recipient' => $this->recipient,
'message' => $this->message,
];
}
}
class MqttEventListener
{
public function __construct(ConnectionManager $connectionManager)
{
}
public function handle(ShouldBroadcastViaMqtt $event)
{
$connection = $this->connectionManager->connection();
$connection->publish('some/topic', json_encode($event->data()));
}
}
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
ShouldBroadcastViaMqtt::class => [ MqttEventListener::class ]
];
}
Just keep in mind that when using a QoS > 0 for publishing, this will not work without running $connection->loop()
(with appropriate arguments) in the handler, which may block the execution until the message was received [and forwareded] by the broker. But you can, of course, also use a queued event listener for this.
Hi, thanks for your answer and sorry the late, was a busy weekend... I've a Laravel app that works with a few users, but mostly with IoT devices. Today is as you say, websockets is broadcasting events to users and devices, but I'm looking forward switch to EMQX as broker, it have is own javascript library for users client (and also offer websockets native), C++/Python libraries and of course MQTT, for IoT devices.- So, I want to use the Laravel events queues benefits when publishing to EMQX.-
Now regarding to your other questions, as you said, should be mapping channels to MQTT topics and with prefix to make it parameterizable. Follow the Laravel (Pusher and Redis) convention, for data format, is the way, I think.-
Also noticed you said, with listeners publishing to EMQX (also queueing this) could be one way.- I'll give it a try when I reach that point, now, I'm migrating lo Laravel 9.x
Hi, Thanks for this greats libraries. I was hoping to use as broadcast driver in laravel, it's possible? Thx