samedi 11 novembre 2017

Websockets or REST for multi-user CRUD

I'm building a multi-user CRUD app where multiple users can delete/create/update products in real-time. But I'm debating whether I should use websockets or REST calls. I've read a ton about the advantages and disadvantages of both, but usually people mention only extremes (websockets are good for heavily real-time applications like multiplayer games, while REST is good for CRUD apps with only a user).

But my app is a bit in the middle. What I mean is that it's a CRUD app, but multiple users can act on the same resources at the same time. So if a user #1 deletes product X in a RESTful app, the product would still be visible for user #2 unless he refreshes the app. So I decided to try websockets, but then I worried this might be overkill.

As a case study, I studied how Google Music handles the removal/upload of songs on multiple devices. Even though updates can only be made by a single account, they use long-polling (which serves the same purpose as websockets) to sync changes across sessions and devices in real-time. If they use it for single-user applications, maybe my use case isn't too overkill after all?

Another concern is that in order to reduce the latency and improve the user experience, my app only fetches all products from the server during the initial webpage load. When I click a link to open another section of the application, it only changes the DOM but keeps the data in memory (via react redux). This means that if I were to use a RESTful approach, the app of a user would be outdated until he refreshes the website manually, which he might never do. A websocket would enable me to update only a few products at a time and keep the app in sync, which seems more efficient than fetching all the products every time the current page changes.

Would it be considered a "bad practice" to use websockets (or long-polling) for CRUD actions, a bit like Google Music does for adding/removing songs? I find that websockets add a lot to the user experience and make it feel more like a desktop or mobile app.




Aucun commentaire:

Enregistrer un commentaire