Can I use useSubscription from liveProvider to receive event from server instead of client side?
Can I use useSubscription from liveProvider to receive event from server instead of client side?
2 Replies
Hi @tingadev. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Yes, you can use the
useSubscription
hook from the liveProvider to receive events from the server side instead of the client side. This is actually the recommended approach for handling real-time updates in refine.
The useSubscription
hook is designed to subscribe to server-side events. It calls the subscribe
method of the liveProvider, which sets up a connection to listen for real-time updates from the server.
Here's an example of how you can use useSubscription
:
In this example, the hook subscribes to a specific channel and listens for certain types of events. When an event is received from the server, the onLiveEvent
callback is triggered, allowing you to handle the event as needed.
It's important to note that while useSubscription
is used on the client side, it's actually subscribing to events that are published from the server. The server is responsible for sending these events, and the client (using useSubscription
) is just listening for them.
This approach is more secure and efficient than publishing events from the client side. As mentioned in the Live Provider documentation, "Publishing on the client side must be avoided generally. It's recommended to handle it on the server side."
For more detailed information on how to use useSubscription
, you can refer to the useSubscription documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@761921731505946634> Kapa.ai is still learning and improving, please let me know how I did by reacting below.