Need help with Socket.io and Next.js
Answered
Japanese flying squid posted this in #help-forum
Japanese flying squidOP
please read my case here: https://stackoverflow.com/questions/78410047/how-to-emit-socket-message-in-server-side-next-api-request
Answered by Toyger
overall your logic is fine, but platform like vercel,render,netlify running as shortlived containers, so most likely it shouldn't work as intended at least for a long time, for short time connection will probably remain, but for a long time it will probably kill server if there is no new incoming connection, so you need to test it yourself.
22 Replies
American Crow
@American Crow Click to see attachment
Japanese flying squidOP
I am hosting it on custom server and it'salready working.problem is that I need to emit message from API Route
Toyger
I didn't tried it, but in this article https://medium.com/@farmaan30327/implement-web-sockets-with-nextjs-and-api-routes-6a14916e6c1a in step 3 they show message can be emitted directly from api route, because it's basically same nodejs runtime , so you can emit some custom event from it, and then in your customserver where you have websocket server logic you will handle this message from your api route and do whatever you need.
@Toyger I didn't tried it, but in this article <https://medium.com/@farmaan30327/implement-web-sockets-with-nextjs-and-api-routes-6a14916e6c1a> in step 3 they show message can be emitted directly from api route, because it's basically same nodejs runtime , so you can emit some custom event from it, and then in your customserver where you have websocket server logic you will handle this message from your api route and do whatever you need.
Japanese flying squidOP
I've tried it but it didn't work. But I've done that the same way, only difference was that in client component I was importing socket from socket.ts , and it wasn't working, can it be the reason?
@Japanese flying squid I've tried it but it didn't work. But I've done that the same way, only difference was that in client component I was importing socket from socket.ts , and it wasn't working, can it be the reason?
Toyger
we can ignore client components for now, only things that important is route handler and custom server, client components right now not a problem.
@Toyger we can ignore client components for now, only things that important is route handler and custom server, client components right now not a problem.
Japanese flying squidOP
okay, from server route it worked, it's also working in server.js ( it loggs that event is emmited)
This is my socket "catcher" component, which should refresh router on socket message. but it doesn't log "Received socket event" at 18th line
oops it works 😄
sorry guys
will it work the same way on render server
correct?
Toyger
client side yes, server side no, render same serverless that don't support long living apps like websoccket servers
@Toyger client side yes, server side no, render same serverless that don't support long living apps like websoccket servers
Japanese flying squidOP
but socket's are working on my render server, so it will work too, right?
I mean when I send socket event from client, server.js catched it and emmites new message
but with this implementation, won't it work on render?
Toyger
overall your logic is fine, but platform like vercel,render,netlify running as shortlived containers, so most likely it shouldn't work as intended at least for a long time, for short time connection will probably remain, but for a long time it will probably kill server if there is no new incoming connection, so you need to test it yourself.
Answer
Japanese flying squidOP
I tested it and in server.js it can't catch socket emmitted from API Route. Any other ideas? ( I've found out work around but it's the best and safest practice )
Toyger
is it working locally?
theoretically you should while socket connected before you emit messages
theoretically you should while socket connected before you emit messages
io.on("connection", (socket) => {
socket.emit("hello", "world");
});@Toyger is it working locally?
theoretically you should while socket connected before you emit messages
js
io.on("connection", (socket) => {
socket.emit("hello", "world");
});
Japanese flying squidOP
Yes, locally everything works perfect
On render, the problem should be exactly what you said about shortlived containers above
@Japanese flying squid On render, the problem should be exactly what you said about shortlived containers above
Toyger
if it's about this then you can't do anything about that on render, you only need to self-host on some vps.
or instead your own websockets use pusher or other alternative.
or instead your own websockets use pusher or other alternative.