I'm trying to have some custom logic when a connection is opened. I need to be able to keep track of different sets of clients. As is, there doesn't seem to be any way to have an event handler for the connection event on the WSS.
This is the example shown in the README:
// app/api/ws/route.ts (can be any route file in the app directory)
export function SOCKET(
client: import('ws').WebSocket,
request: import('http').IncomingMessage,
server: import('ws').WebSocketServer,
) {
console.log('A client connected!');
client.on('message', message => {
client.send(message);
});
client.on('close', () => {
console.log('A client disconnected!');
});
}
Any code that isn't inside of an event handler is run when a connection is opened. Since the connection is automatically opened when useWebSocket() is ran on the client side, I'm not able to provide the WebSocket server with the information it needs to distinguish between clients without making sending a message after the connection, which is messy and feels unnecessary.
Would it be possible to support adding open/connection event listeners to the server? Perhaps a mirror to the close listener?
I'm trying to have some custom logic when a connection is opened. I need to be able to keep track of different sets of clients. As is, there doesn't seem to be any way to have an event handler for the
connection
event on the WSS.This is the example shown in the README:
Any code that isn't inside of an event handler is run when a connection is opened. Since the connection is automatically opened when
useWebSocket()
is ran on the client side, I'm not able to provide the WebSocket server with the information it needs to distinguish between clients without making sending a message after the connection, which is messy and feels unnecessary.Would it be possible to support adding open/connection event listeners to the server? Perhaps a mirror to the
close
listener?