Reputation: 439
I've been using NextJs on Vercel for a while now because of its amazing development experience and performance but I didn't need to use real-time data with WebSockets in any of my applications until now.
At the moment, however, I'm working on a new project that needs real-time but I'm struggling to decide how to implement WebSockets.
To give a little bit of context, this application is a real-time trading market where users exchange virtual assets for money. Therefore, there's a need for WebSockets to keep clients updated on new listings on the market, as well as a background job running every few seconds to track if trades were completed, canceled, etc.
Keeping in mind that I would like to keep using NextJs hosted on Vercel, my question is: should I create a separate server to handle all real-time data and background jobs while using NextJs or should I go with the normal React + ExpressJs + WebSockets server? I've also thought about using AWS services but I think it would be tricker than the other two alternatives.
And If I were to create a separate server, would it be ideal to use Serverless Functions (created on Vercel) to do database operations and then notify the WebSocket server to replicate the changes (e.g. create a new item listing)? Or should I use the WebSocket server's routing (ExpressJs routes) to perform those types of tasks that need to be replicated to the client?
Upvotes: 5
Views: 2873
Reputation: 3145
Vercel themselves suggest not to use their serverless functions for realtime communication
Serverless Functions on Vercel are stateless and have a maximum execution duration. As a result, it is not possible to maintain a WebSocket connection to a Serverless Function.
They provide a few realtime alternatives including pusher which has a step by step guide https://vercel.com/guides/deploying-pusher-channels-with-vercel
Upvotes: 2