Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom server #9

Closed
wants to merge 4 commits into from
Closed

Conversation

apteryxxyz
Copy link
Owner

@apteryxxyz apteryxxyz commented Nov 5, 2023

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}`);
    });
});

@apteryxxyz apteryxxyz linked an issue Nov 6, 2023 that may be closed by this pull request
@apteryxxyz apteryxxyz self-assigned this Nov 6, 2023
@GGAlanSmithee
Copy link

GGAlanSmithee commented Nov 7, 2023

@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
Copy link
Owner Author

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
Copy link

Will this merged to main branch soon?

@sanyam810
Copy link

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
Copy link
Owner Author

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

@apteryxxyz apteryxxyz closed this Jun 30, 2024
@apteryxxyz apteryxxyz deleted the experimental-custom-server branch July 1, 2024 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Usage with a custom server
4 participants