mutate ws
object?
#49
-
Hey Alex, thanks for publishing this, glad to see v15 available and appreciated as always. Quick question, with other WS schemes we generally would attached handlers when a request is opened something like
then we had to do something along these lines with your previous function registerCallbacks(ws, client) {
ws.on('message', message => client.handleMessage(message));
ws.on('close', client.handleClose);
ws.on('error', client.handleError);
ws.on('pong', client.handlePong);
} In the new API we don't really have that, so is the idea that we would instead mutate the WebSocket object so we can store client-specific data as-needed? I noticed in the I did notice the Also want to make sure that object isn't used in some way and I should restrict to just storing simple data like a clientID or something or if I can/should just store a full-on client state there. On another note, I noticed you can't really attach listeners for In the past I'd use this to monitor clients, i do see we now have the idle timeout which could be an ideal solution to this anyway, but just wondering! I do see that you are handling the Thanks again!! Appreciate the efforts! Excited to see what benefits using the http portions of this will yield as well! Just to outline my thinking in a simple way let i = 0;
UWS.App()
.ws('/*', {
open: (ws, req) => {
ws.id = i++
},
message: (ws, message, isBinary) => {
console.log('Message from client id: ', ws.id);
},
close: (ws, code, message) => {
console.log('WebSocket closed ', ws.id, code, message);
},
}) If that is for any reason not recommended the other method would be something more like the below, which would clearly be far less performant: const clients = new WeakMap();
let i = 0;
UWS.App()
.ws('/*', {
open: (ws, req) => {
clients.set(ws, {
id: i++,
});
},
message: (ws, message, isBinary) => {
console.log('Message from client id: ', clients.get(ws));
}
}) Lastly, how would we go about getting some information on the underlying socket? For example, I used to do something along these lines to capture the connecting clients remote address: const { headers = {} } = ws.upgradeReq;
let ip;
if (headers['true-client-ip']) {
ip = headers['true-client-ip'];
} else {
const { remoteAddress } = ws._socket;
ip = remoteAddress ? trimLeft(remoteAddress, '::ffff:') : 'unknown';
} I see how to get the header so that one is simple enough! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hello, Many good questions here. Yes, you can and should mutate the ws object (and res object) in any way you want. Attach anything to it you want. It will be valid from open event to close event. Res object is valid from request event to res.end or res.onAborted or res.tryEnd did succeed. Getting the IP is WIP and will be added. Sending ping is also WIP. Errors, we don't have errors. You don't need to care more than to handle open/close events. |
Beta Was this translation helpful? Give feedback.
Hello,
Many good questions here.
Yes, you can and should mutate the ws object (and res object) in any way you want. Attach anything to it you want. It will be valid from open event to close event. Res object is valid from request event to res.end or res.onAborted or res.tryEnd did succeed.
Getting the IP is WIP and will be added.
Sending ping is also WIP.
Errors, we don't have errors. You don't need to care more than to handle open/close events.