apteryxxyz / next-ws

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

Support : Where to include sslOptions when initializing SOCKET #25

Open sanyam810 opened 6 months ago

sanyam810 commented 6 months ago

I'm currently working on a WebSocket server using next-ws in a Next.js environment. I need to include SSL options (Want to include ssl certificate & key) when initializing the WebSocket server , but I'm unsure how to do this properly.

import { NextRequest, NextResponse } from 'next/server';
import WebSocket from 'ws';

type DrawLine = {
    prevPoint: Point | null;
    currentPoint: Point;
    color: string;
    x: number;
    y: number;
};

const clients: Set<WebSocket> = new Set();

function SOCKET(
    client: WebSocket,
    request: import('http').IncomingMessage,
    server: WebSocketServer,
) {
    // WebSocket server logic
}

function GET(req: NextRequest, res: NextResponse) {
    // Handle GET request
}

function POST(req: NextRequest, res: NextResponse) {
    // Handle POST request
}

export { SOCKET, GET, POST };

I need guidance on how to properly include SSL because the connection to WS don't get established when in production environment

apteryxxyz commented 3 months ago

Using a custom server where you create your own WebSocketServer instance and tell Next WS to use that, you could pass additional options there.