Should I Use WebSockets Alongside REST for Notifications?
Asked by
102 puppet shows
I'm transitioning to backend development and currently building an API with Python (Flask) for a project that handles most of our site's traffic. We've encountered a situation where we need various types of notifications in the app. I thought of using WebSockets, but I'm not experienced with them. I'm considering the Flask-SocketIO library to create separate channels for each user to receive notifications. However, I have concerns about whether this is the right approach, especially regarding performance and concurrency. Can REST and WebSockets coexist in the same service, and how would that impact performance? Is it better to separate them into different servicesโone for WebSockets and another for API calls?
Answers
Reply to this answer
WebSockets are great for scenarios where you need real-time communication, like sending notifications from the server to clients without them having to request it. But yeah, they introduce some complexity because they make your application stateful, which is different from the traditional stateless nature of HTTP. If your app just needs occasional updates and the timing isnโt super critical, polling could still work fine. But if you go with WebSockets, keep them limited to just notification delivery. You can implement identity tokens to track whoโs connected, and use WebSockets specifically for notifications while relying on REST for other API interactions.
Reply to this answer
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions