sockets in app directory
Answered
Sun bear posted this in #help-forum
Sun bearOP
how can i manage to use sockets with app directory
Answered by fuma
Websocket? Unfortunately, Next.js is built for serverless, which means you can’t run a websocket server without using a custom node.js server. Custom server isn’t allowed by platforms like Vercel, also not a recommended way.
Generally, we will use 3rd party services, such as Ably Realtime. They work great for serverless environments. Websocket needs an actual persistent server, and Next.js doesn’t come with that by default.
Generally, we will use 3rd party services, such as Ably Realtime. They work great for serverless environments. Websocket needs an actual persistent server, and Next.js doesn’t come with that by default.
15 Replies
Sun bearOP
Websocket? Unfortunately, Next.js is built for serverless, which means you can’t run a websocket server without using a custom node.js server. Custom server isn’t allowed by platforms like Vercel, also not a recommended way.
Generally, we will use 3rd party services, such as Ably Realtime. They work great for serverless environments. Websocket needs an actual persistent server, and Next.js doesn’t come with that by default.
Generally, we will use 3rd party services, such as Ably Realtime. They work great for serverless environments. Websocket needs an actual persistent server, and Next.js doesn’t come with that by default.
Answer
@Sun bear Click to see attachment
if you're hosting this nextjs app in a serverful environment then yes you can keep it in that file else you will need custom backend if you wanna host on vercel
@fuma Websocket? Unfortunately, Next.js is built for serverless, which means you can’t run a websocket server without using a custom node.js server. Custom server isn’t allowed by platforms like Vercel, also not a recommended way.
Generally, we will use 3rd party services, such as Ably Realtime. They work great for serverless environments. Websocket needs an actual persistent server, and Next.js doesn’t come with that by default.
Sun bearOP
I need free server which one can i use
is Ably Realtime free?
Yep
Sun bearOP
thanks
so i can build express server and connect it to nextjs app
Correct
But considering the cost to host server, maybe simply using a service is better for small projects
Sun bearOP
i need it for small chat application
I built Shark chat, it’s a simple chat app, and it’s using Ably
@Sun bear i need it for small chat application
you can use something like pusher for that
The price of Ably Realtime seems to be cheaper, you can take a look to their pricing
Sun bearOP
i dont know what is required for the task actually i have to ask and then i will see which service or platform i will use