How to properly use sockets Join, Emit, Direct Message and Broadcast to refresh API like Refresh Server Action

The use of “Refresh Server Action” is not the best for multiusers business model, wors if it’s a SaaS model, because not all users need to refresh the server action at same time, this cause big performance issues.

Now the questions are, how to obtain similar results using Join and Emit, honestly I have too much questions about it:

When the Socket “connect” should be use, the same for “disconnect”. This both can be omitted creating a new socket action?
What are Namespace and how to use it, useful if the example is not about “Realtime messaging”.
How to update an Server Connect API based on new updates just like “Live Refresh with Sockets” works.
Can one user join to multiple rooms, e.g. User “One” Join to Room “A” and “B” and “C”. Then the Room “A” be able to send updates of API’s to Room “B”. How to do that?

Thanks

Users can join multiple rooms.

In my case I join every user to their own company id, so that I send updates relevant to that company id to their users. For the realtime messaging, you can simply make two users join a room, and send the updates to that room.

1 Like

Thank @htatd

But, honestly I’m still confuse.
When you say “send updates relevant to that company id” you mean to refresh user API to update the data that is viewport in the user browser or its send a simple message like notification message?

For example, I want to update the API (Server Connect) from a online user that belongs to a group (company/business ID) or maybe same group based on roles. If a client send a form, the user business should be able to see real time changes of the table that is set up using “Boostrap 5 Table Generator”. In the same way like “Live Refresh with Sockets” works.

So, how to update user business table when a client submit a form.
How to translate the use of “Refresh Server Action” and “Live Refresh with Sockets”" using Join, emit, etc.
The only examples that exist in forum are about Real Time Messaging, but I dont want to send message from user A to B, I want to update or refresh the server connect to update the data of that users just like “Live Refresh with Sockets” works.

Sorry, maybe its something very easy, but I cant figure out how to do that.

Thanks

Namespace would probably be the proper way to deal with multi-tenancy but I don’t think there is a way to have a dynamic list of namespaces within the basic Wappler setup. If you had a defined list of tenants and didn’t mind pushing code to update, then I suppose you could use them.

So I’ve also dealt with your multi-tenancy by using Emit, and then letting the client side decide if it should act upon the socket message.

For example, here is a Socket (under Sockets on workflow panel) that simply specifies 2 data points

Screenshot 2024-01-04 at 3.30.43 PM

Then in a workflow, I use an Emit action and specific the type to match the socket above and then set the two data points:

Then on the main layout I handle the inbound socket messages:

Screenshot 2024-01-04 at 3.35.41 PM

And I evaluate the $event message to decide if something should be done. In this case, I only refresh the server connect if it is for the current tenant and the current meal plan:

This obviously doesn’t prevent extra socket messages from being sent to all clients logged in, but it does prevent the server connect refreshes that would happen if using the Refresh with Sockets (which really should only be used for app wide data.)

It’s a fairly simple way to deal with the heaviest part of the problem, too many server connect refreshes.

I’m not using the client side (app connect) on Wappler for a while now so I don’t remember the terms exactly.

Basically all you have to do is identify the users you want to emit/send messages to, which you can do with the rooms, or you can save the socket identity of the user in a db table with the user id.

Then all you have to do is emit messages to the room, or to the socket id directly. Then on the app connect side you have to process the socket message, you can then either fully refresh the relevant server api, or you can directly modify the data (in this case you have to create the parameters in the socket message).

Apologies if that still doesn’t answer your question, and perhaps I’m not understanding where you struggle exactly