Skip to content

Error while Setting-Up Socket.io-based Serverless API Route #8309

@sorleone

Description

@sorleone

Questions

  • Is this how we should add Socket.io to a serverless API route?
  • Can that be done without requiring http and/or micro@9.3.5-canary.1 or any other version of it (without that at all)?
  • Can Next.js accept trailing slashes after route names?

Error

When connecting keeps returning 404 not found becasue Socket.io adds a slash after route.

http://localhost:3000/api/route/?EIO=3&transport=polling

Whereas Next.js would only accept something like this apparently:

http://localhost:3000/api/route?EIO=3&transport=polling

Code

./pages/api/route.js

const http = require('http');
const micro = require('micro');

const server = micro((req, res) => {
  console.log('endpoint connection')
});

const options = { path: '/api/route', cookie: false };
const io = require('socket.io')(new http.Server(server), options);

function connection(socket) {
  console.log('socket.io connection');
}

io.on('connection', connection);

export default server;

./pages/index.js

[...]
const socket = io('http://localhost:3000', { path: '/api/route' });
[...]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions