apteryxxyz/next-ws

How to access to params using dynamic route

Opened this issue · 7 comments

I thinks everytings is on title

The second parameter passed to the SOCKET function is an IncomingMessage object from the Node.js http module. You can use that to get everything related to the request including the URL.

Ok ! Thanks but why do you think about having something like that:

export function SOCKET({
  client: import('ws').WebSocket,
  request: import('http').IncomingMessage,
  server: import('ws').WebSocketServer,
  params: YourParamsType
}) {
  // ...
}

So parameter of function is one object. Like Get methods of next

@apteryxxyz dyamic path routes are not working using next-ws, it throws 'could not find module for page /api/socket/history' this is in path /api/socket/[api_type]/route.ts file, it doesnt read this path

@apteryxxyz dyamic path routes are not working using next-ws, it throws 'could not find module for page /api/socket/history' this is in path /api/socket/[api_type]/route.ts file, it doesnt read this path

Fixed in next-ws@1.1.1.

Speaking of this issue, I'm gonna go ahead and close it, you can get the params from the request parameter. I want to avoid adding more parameters to the SOCKET function, and I'm not even sure if next-ws can access the names of dynamic segments.

@apteryxxyz,
I think it could be easy to implement passing dynamic route params to SOCKET handler.
I started using next-ws today and after digging into how it works (Genius idea btw) I think the solution would be:

  1. In server\helpers\next.ts - resolveFilename - collect all keyParts and pathParts where isDynamic === true. Instead returning path only return object { path, params }
  2. In server\setup.ts - setupWebSocketServer pass it to the SOCKET handler (in next-js form so as { params: Promise<...>}

I can look at it once I have some time to figure out how to test it locally

PS: Sorry for commenting closed issue
PS2: Next-ws is great tool
PS3: It works with Next 15.0.4

@Morcatko I'll revisit this issue after Christmas, have reopened for now