Socket refresh heads-up

Hi!

Might be obvious to most, but I made the mistake of using “Refresh Server Action” for everything, which caused big performance issues. When developing, it’s all fine as you’re the only one testing the app, but when I started to have more users I noticed the problem.

Screenshot 2022-06-23 at 16.17.24

When I wanted to refresh data for 1 user, and I used “Refresh Server Action”, it was obviously refreshing ALL user’s api’s, which basically froze the app for everyone.

So when a refresh is needed:

Use Emit for a group of users that you want to target (for example if all users within a company need an update):

  • In my case when a user connects, it connects to the room of his company id
    Screenshot 2022-06-23 at 16.37.34

So when I want to refresh all users from 1 specific company, I emit to the company room:

Use Direct Message to target just one user

  • When a user connects to the app, I update a database table with the user’s socket id, which I look up when needed and send a direct message to only his socket id

Then in the front-end, you can use the socket component to handle the socket messages for the specific socket events, and run the database refreshes:

So again, probably obvious, but I wanted to spare you the time of re-doing all this like me in case you just got started with developing with only the “Refresh server action” :sweat_smile:

6 Likes

You can find info about emit, broadcast and direct messages here:

1 Like

I remember having that Aha moment as well. :rofl:

2 Likes