laravel / reverb

Laravel Reverb provides a real-time WebSocket communication backend for Laravel applications.
https://reverb.laravel.com
MIT License
1.02k stars 71 forks source link

[1.x] Allow to listen for MessageReceived event #134

Closed rihardsgrislis closed 4 months ago

rihardsgrislis commented 4 months ago

Hey,

Love using Reverb so far! 🚀

I would like to use the websocket server to receive non-Laravel messages from other clients as well and handle the messages in my app. I have some IoT devices that allow for outbound websocket. The MessageReceived event is already implemented and I can just listen for it in the app. However it doesn't get executed because if the message doesn't contain the "event" key the server fails handling the message (Undefined array key "event") and sends 'event' => 'pusher:error' event to the client with message "Invalid message format".

We just need to check for the "event" key before using the Pusher or ClientEvent handler.

joedixon commented 4 months ago

Thanks for the pull request, but right now the plan is to support solely the Pusher protocol.

There is scope within Reverb to provide support for additional protocols, so I could see a future where we ship a plain WebSocket server, but right now, the focus is on Pusher only.

rihardsgrislis commented 4 months ago

Hope you reconsider. Seems like a wasted opportunity to add great general purpose tool in the Laravel toolkit. It doesn't seem to add any maintenance overhead and worked perfectly with this single change. A good trade-off to allow to receive messages and process them in the same Laravel app from various devices and clients.

rihardsgrislis commented 4 months ago

I would also like to point out that this change just allows the data, that is already received and parsed by the websocket server and available, to be used (listened to) by everybody.

rihardsgrislis commented 4 months ago

Laravel Socialite for example allows to implement your own providers along the Laravel provided ones. It should be the case for Reverb too.

rihardsgrislis commented 4 months ago

"I have never written a single private class or method in an OS repo in my entire career

Like free speech, open source code is only truly open if someone you don't like can use and extend your code in ways that you disagree with, and you can't do anything about it" - Steve Bauman

Let developer listen to the message received event for all types of messages. There can be a new MessageHandled event for those that are handled by Reverb.

driesvints commented 4 months ago

@rihardsgrislis feel free to fork this project and modify this entirely in the way you want 👍

rihardsgrislis commented 4 months ago

@driesvints I rest my case now. This websocket server is too awesome. Together with FrankenPHP running one is such a delightful developer experience and I never want to go through something else. So, yeah, I'm forking it already.

websocket:
    image: ghcr.io/rihardsgrislis/my-app/app:main
    container_name: reverb_websocket
    working_dir: /app
    command: [ "php", "artisan", "reverb:start" ] # , "--debug"
    restart: unless-stopped
    ports:
      - "8080:8080"