laravel / reverb

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

[1.x] Added new events #91

Closed michaelnabil230 closed 7 months ago

michaelnabil230 commented 7 months ago

This PR introduces significant enhancements to events, providing developers with the capability to listen to a range of pivotal events, thereby facilitating more dynamic and interactive user experiences. Additionally, it's noteworthy that events do not block the event loop to close the connection, ensuring smooth operation and efficient resource management. Moreover, this PR assists developers in migrating from Soketi to Reverb, streamlining the transition process, and leveraging the enhanced event capabilities offered by Reverb. The following events are now accessible through this PR:

Events:

  1. NewConnection:

    • This event enables developers to track and handle new connections established within the application. It empowers them to execute specific actions or initialize processes upon a new client connection, enhancing overall control and management of interactions.
  2. ConnectionClosed:

    • Developers gain insight into connection terminations through the ConnectionClosed event. This visibility allows for graceful handling of connection closures, ensuring necessary cleanup tasks are performed to maintain application stability and reliability.
  3. ConnectionError:

    • The ConnectionError event notifies developers of connection errors during their lifecycle. By detecting these errors, developers can implement appropriate error-handling mechanisms promptly, improving the robustness and resilience of Laravel applications.
  4. SubscribedToChannel:

    • Signifying a client's successful subscription to a channel, the SubscribedToChannel event empowers developers to monitor subscription activities. They can execute custom logic or trigger additional actions tailored to specific user interactions, enhancing real-time messaging experiences.
  5. UnsubscribedFromChannel:

    • Conversely, the UnsubscribedFromChannel event indicates when a client unsubscribes from a channel. Developers can track these unsubscriptions and perform corresponding procedures, such as updating UI elements or performing cleanup tasks, ensuring efficient management of application resources and facilitating seamless user experiences.
  6. ServerStarted:

    • The ServerStarted event notifies developers when the server has started successfully. This event is crucial for executing tasks that should occur only after the server has fully initialized and is ready to handle incoming connections or requests.
  7. ServerStopped:

    • Conversely, the ServerStopped event indicates when the server has stopped. Developers can utilize this event to perform cleanup tasks or execute finalization procedures before the server completely shuts down, ensuring proper resource management and data integrity.

By incorporating these event functionalities, developers can unlock a myriad of possibilities for building dynamic, responsive, and engaging web applications. Whether it's real-time chat applications, live notifications, or collaborative features, this PR equips Laravel developers with the tools needed to craft immersive user experiences that keep users connected and informed in real-time.

mpociot commented 7 months ago

The main problem with these events is that even if dispatching the event might not block the event loop, any IO work that you do in the event listeners certainly will. So introducing these events basically opens up the door to easily (without knowledge) block the event loop/websocket server.

michaelnabil230 commented 7 months ago

Hi @mpociot Can you explain more, And from your point of view what is the correct way so that I can listen to an event?

joedixon commented 7 months ago

Thanks for the PR @michaelnabil230. However, right now, we want to keep events to a minimum to prevent users from unintentionally blocking the server.