Websockets vs REST-api when having a interval?

I have a endpoint like /resource/{id}/rows routing to a function async fn get_rows(...) which would return all the rows of resource with id every 2000ms. The user has a panel which shows all the rows live (without reloading).

Is there a advantage in using websockets? I feel that if I only check it every 2000ms I could as well just use setInterval(..., 2000) in Javascript executing a REST-API call to retrieve the rows instead of using websockets?

The point is to make it as fast and efficient as possible.

Well, this pretty much sums it all. Performing a request every 2000ms has, comparatively speaking, a lot of overhead when compared with a single websocket connection.

Overall, I think you are asking the wrong questions. Rather than focusing on "making it as fast and efficient as possible" you should think about what makes more sense from a UX and TTM perspective.

How many users will the app need to support? How long will the user remain in this dashboard view? If, let's say, the user will be there for a minute or two at most, why would the performance of the implementation bother you? KISS.

Typically my first thought would be to use requests to a rest API for what you describe. Why do anything more complex? (See below)

I think of web sockets as being appropriate when events are happening at the server end at unknown, random, times and should be notified to the clients. That saves the client endlessly polling the server with API requests when nothing has happened.

Except, I have used web sockets in situations like yours when data is being updated frequently. Perhaps not necessary but I just felt icky thinking of making all those rest requests unnecessarily. But usually I had faster streams of updates to deal with than you describe.

I guess it's a trade off only you can judge for your situation.

1 Like

imho no general answer for everyone - do you need to worry about amount of data carried over? if not, do JS/TS setTimeout REST GETs and save on your expensive programmer's time :slight_smile:

Otherwise you can have fun (not meaning this sarcastically) synchronizing, sending over diffs/updates instead of full data / comet model / etc.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.