4

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?

tavin-dev
  • 321
  • 3
  • 10

1 Answers1

2

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

Ramakay
  • 2,919
  • 1
  • 5
  • 21
  • 1
    I didn't say I would be using Vercel's Serverless Functions for real-time communication. Those serverless functions would do database operations and then somehow call the WebSocket server and then this server would send WebSockets events. And those real-time alternatives they suggest would not suit my use case. I need to run background jobs every few seconds to track trades and notify the client of every update. So having a server to run those jobs and send WebSockets events is probably necessary. – tavin-dev Mar 18 '22 at 14:07