apteryxxyz / next-ws

Add support for WebSockets in Next.js app directory.
https://npmjs.com/next-ws
131 stars 9 forks source link

Add support for custom server #9

Closed apteryxxyz closed 2 days ago

apteryxxyz commented 8 months ago

I've just published an experimental version of Next WS (next-ws@experimental) that adds support for custom servers. It appears to work but it's not fully tested.

The way it works is by you assigning your own http server (and optionally ws server) to a global variable, which Next WS will check for when setting up the server. I'm not a fan of assigning to global, but I prefer this over having to patch more things in Next.js.

CustomHttpServer, CustomWsServer are two symbols strings that get exported from next-ws/server and can be used to assign your custom http server to the global object.

This is the setup I used that works:

// server.js
const { Server } = require('http');
const { WebSocketServer } = require('ws');
const { parse } = require('url');
const next = require('next');
const { CustomHttpServer, CustomWsServer } = require('next-ws/server');

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = 3000;

const httpServer = new Server();
global[CustomHttpServer] = httpServer;
const wsServer = new WebSocketServer({ noServer: true });
global[CustomWsServer] = wsServer;

const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  httpServer
    .on('request', async (req, res) => {
      const parsedUrl = parse(req.url, true);
      await handle(req, res, parsedUrl);
    })
    .listen(port, () => {
      console.log(` ▲ Ready on http://${hostname}:${port}`);
    });
});
GGAlanSmithee commented 7 months ago

@apteryxxyz not sure if related, but when running another - totally unrelated project, based on next.js 14 but not using next-ws (or a custom server) I am getting this error

Error: Cannot find module 'next-ws/server'

I can't remember seeing this before, hence why I add this as a comment in this PR, even though it might be unrelated (in which case, I can create a new issue instead)

to replicate, simply create a new next-js project, run npm i and npm run dev without installing next-ws. I can work around this for now by installing next-ws in the project

apteryxxyz commented 7 months ago

In that case I think Next WS patches the cached/linked version of the package. Not too sure of a way to resolve this, other than just maybe adding a try catch to where Next WS is required.

chinna2580 commented 5 months ago

Will this merged to main branch soon?

sanyam810 commented 3 months ago

Is there a way to include SSL options such as adding an SSL certificate and key, during WebSocket server initialization? Doesn't necessary need to be a custom server. I have opened a issue for it, would really appreciate if you share some leads on it.

apteryxxyz commented 2 days ago

This branch is being closed in favour of major-improvements.