General question about Vapor

First of all compliments for the book. I am working through and I am learning a lot, mostly from using futures. After finishing the book I want to create my first small vapor project and I have a question regarding it. Perhaps somebody can give me a hint.
How would you implement this scenario. I would create a vapor api with an database, where every user has a table with its own data or even its own database. AS frontend I would create a SPA using React. Now my question. I would like that if i have two devices that work on the same data and if the data in the backend changes the other device receive the changes immediately without sync or refresh button. I mean the server should notify the connected devices, only the user of the corresponding table.
My idea were two:

  • (Preferred idea) Using websocket as described in the book. On a change in the table, notify the client with his userId and the client checks if its his userId and then the client fetches his data.
  • The client is polling the server every 2 seconds and asks for changes. But I think this not a good idea.
    Or there exists another method to notify the client, that data changed in the meantime.
    I know that there exists databases like Google Firestone, that have this feature on board, but this costs money and I want create only a small project for my own using Vapor and MySQL.
    I know that this question refers not directly to the book, but perhaps nevertheless somebody can give me a tip, as here are some Vapor(Backend) experts.
    Thanks,
    Arnold

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

Yep your two options are either polling with JavaScript or using WebSockets to push updates or notifications to the clients. Either should work I think and React should provide tools for doing both. The JavaScript option will be the simplest as you’re just polling the usual endpoint. Websockets will be a little more involved and you need to decide if you want to push the updated data or just a notification to get new data. It’s probably the more ‘elegant’ solution but will require more work.

Hope that helps!

Thank you Tim, I will try to do it with websocket. First I have to read the chapter about in the book and then I will try it using websockets with notification.
Arnold

1 Like