Socket.io is not working in Next Js
Unanswered
Velvet ant posted this in #help-forum
Velvet antOP
I'm trying to use the socket.io in the next js application ( "next": "^13.4.12",) but it is not working. I have a testing code of pages/chat and page/api/socket.ts files.
Please check my attached code and the response of the console inside the client browser and the server response inside the terminal.
Thank you!
Please check my attached code and the response of the console inside the client browser and the server response inside the terminal.
Thank you!
11 Replies
@Velvet ant To my knowledge,
socket.io requires a long-run server. And next API is just a serverless function. Thus, you can't use a socket server in nextjs.Velvet antOP
@Nelson Thank you for your response, but my purpose of using the socket is only to fetch the response from the webhook url
I'm using a third-party api in my Next Js project, their API final response is coming inside the pages/api/skiptrace-webhook. The problem is I don't know the solution HOw I will get the response from the webhook because the webhook is called inside the other api as a input or data.
@Nelson Do you know any better solution for it. If I cannot use the socket.io in next js.
I don't know how to implement it due to my poor knowledge of real-time apps.
@Velvet ant <@886269624608522240> Do you know any better solution for it. If I cannot use the socket.io in next js.
Brown bear
You might still be able to use socket.io, but it would have to be external.
Example server:
Example server:
const http = require('http');
const socketIO = require('socket.io');
const server = http.createServer(app);
const io = socketIO(server);
io.on('connection', (socket) => {
//do your stuff here
});
server.listen(8080, () => {
console.log(`Server is running on port 8080`);
});Velvet antOP
@Brown bear I will replace your code with my socket.ts file, inside the pages/api/socket.ts?
@Velvet ant <@886029036512485397> I will replace your code with my socket.ts file, inside the pages/api/socket.ts?
Brown bear
that wont work, you have to have an external server
Velvet antOP
@Brown bear my goal is to get data from there pages/api/core when webhook data comes and will send it into the frontend with the helpf of the socket.io
I understood, you say, I will use node js or php
for it