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

WebSockerServer not firing 'on connection' event #32

Open
FlynnHillier opened this issue May 17, 2024 · 0 comments
Open

WebSockerServer not firing 'on connection' event #32

FlynnHillier opened this issue May 17, 2024 · 0 comments

Comments

@FlynnHillier
Copy link

FlynnHillier commented May 17, 2024

I am trying to establish some listeners on my server using the WebSocketServer that is exposed within the SOCKET route.

Given the following simple code in a route.ts file:

function applyWSSListeners(wss: WebSocketServer) {
  console.log("applying listener");

  wss.once("connection", (socket) => {
    console.log("connection!");
  });
}

export async function SOCKET(
  client: WebSocket,
  request: IncomingMessage,
  server: WebSocketServer,
) {
  applyWSSListeners(server); 
}

I observe that applyWSSListeners is being called, as I can see 'applying listener' being logged in the console when my page is refreshed and the Socket provider makes a request to the route.ts endpoint (as expected).

However, I never see the 'connection!' string logged to the console. I would understand if this perhaps happened on the first connection as 'applyWSSListeners' is perhaps called after the socket connected - however I cannot understand why I still do not see the message logged in any subsequent connections (after applyWSSListeners has definitely been called).

I am sure the sockets are connecting as when I log server I can see the WebSocket objects in a Set.

I created this simple example by following the simple example provided by the ws docs .

Could anyone please share how they've handled setting up listeners on the WebSocketServer object?

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

No branches or pull requests

1 participant