D
Deno

help

Can I add properties to a websocket object?

Ccapo_greco1/19/2024
console.dir (socket.id)
console.dir (socket)

Object.assign (this.control.id, socket.id)
console.dir (socket.id)
console.dir (socket)

Object.assign (this.control.id, socket.id)
I am adding an id object to the sockets to help keep track of them, but something is giving rise to this behaviour, which doesn't seem to make sense
No description
DAHDeno AI Helper1/19/2024
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, I can offer some general JavaScript advice. In JavaScript, you can add properties to most objects, including WebSocket objects, as they are just instances of the WebSocket class which is a type of object. However, there are a few things to note in your code: 1. console.dir(socket.id) - This line suggests that you are trying to log the id property of the socket object. If socket.id is not already defined, this will log undefined. 2. Object.assign(this.control.id, socket.id) - This line is attempting to copy the value of socket.id to this.control.id. However, Object.assign is typically used to copy the values of all enumerable own properties from one or more source objects to a target object. If socket.id is a primitive value (like a string or number), you don't need Object.assign. You can simply assign it directly:
this.control.id = socket.id;
this.control.id = socket.id;
If socket.id is an object and you want to merge its properties into this.control.id, then you should ensure that this.control.id is an object before using Object.assign:
this.control.id = this.control.id || {}; // Ensure this.control.id is an object
Object.assign(this.control.id, socket.id);
this.control.id = this.control.id || {}; // Ensure this.control.id is an object
Object.assign(this.control.id, socket.id);
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. If you continue to experience unexpected behavior, it may be helpful to provide more context or review the surrounding code to ensure that the WebSocket object and the this.control object are being used as intended.
ABAltair 680b1/19/2024
Each new WebSocket Client sends a unique id in its request header. On the server, I use the client id to identify this connection.
let id = '';
// When ready, sends this client its unique id.
socket.onopen = () => {
id = request.headers.get('sec-websocket-key') || 'id'
if (socket.readyState === OPEN) {
socket.send(JSON.stringify({ topic: "Registered", data: { id: id, name: userName } }))
}
}
let id = '';
// When ready, sends this client its unique id.
socket.onopen = () => {
id = request.headers.get('sec-websocket-key') || 'id'
if (socket.readyState === OPEN) {
socket.send(JSON.stringify({ topic: "Registered", data: { id: id, name: userName } }))
}
}
I like to use that as the client.id
I confirm the registration by returning this id to the client. Let me know if you'd like to see the complete WebSocket-Server code. It handles multiple clients.
Ccapo_greco1/19/2024
brilliant - thank you I will bbl to discuss further
ABAltair 680b1/19/2024
I built a gist for you. https://gist.github.com/nhrones/d37855a844e114b801fac545ef1c5c89#file-ws_clients-md I could build you a very simple chat-app in a public repo if that would help you.
Very simple is best if you just want to see the communications work. Also Note: This key is NOT UUID, but will be unique enough for most WS apps.
The sec_websocket_key is calculated by base64 encoding a random string of 16 characters each with an ASCII value between 32 and 127.
A different key will be randomly generated for each new connection.
~39 Trillion IDs needed, in order to have a 1% probability of at least one collision. At 10 IDs per second, would take ~126,000 years to create one collision!
~39 Trillion IDs needed, in order to have a 1% probability of at least one collision. At 10 IDs per second, would take ~126,000 years to create one collision!
Because we use a Map, We could simple detect an ID collision, and just handle it
if (map.has(id)) handleNotUnique();
if (map.has(id)) handleNotUnique();
Ccapo_greco1/19/2024
hi! @Altair 680b ok I am back at my computer I am trying to coordinate across servers with BroadcastChannel, as well as across instances with WebSockets I am also generating memorable names for each server and client instance I am using Maps to keep track of everything is it possible to attach the memorable names etc. directly onto the weboscket objects themselves, or would I need to create an encapsulating object
ABAltair 680b1/19/2024
As I demonstrated in the gist, I would just wrap the socket like below(client), The socket instance then remains optimal and its use is independent of the tracking object. Adding a property to an instance object may impact its overall performance. I would then use either your id or name as the key for the clients map.
/** Websocket client collection */
const clients: Map<string, Client> = new Map()

/** Registers a new WebSocket client
* handles all socket events and messaging
* @param { WebSocket } socket - WebSocket connection
* @param { string | null } id - request.headers.get('sec-websocket-key')
*/
export const connectClient = (socket: WebSocket, id: string) => {
const thisClient = id; // sec-websocket-key

// create the new client object
const client: Client = {
id: thisClient,
name: "",
socket: socket,
};

// register this new client in your collection
clients.set(id, client);
/** Websocket client collection */
const clients: Map<string, Client> = new Map()

/** Registers a new WebSocket client
* handles all socket events and messaging
* @param { WebSocket } socket - WebSocket connection
* @param { string | null } id - request.headers.get('sec-websocket-key')
*/
export const connectClient = (socket: WebSocket, id: string) => {
const thisClient = id; // sec-websocket-key

// create the new client object
const client: Client = {
id: thisClient,
name: "",
socket: socket,
};

// register this new client in your collection
clients.set(id, client);
I'd prefer using the id as it will be unique.

Looking for more? Join the community!

Recommended Posts
Deno + Vite + React + VSCodeCould someone help me setup VSCode so that it recognizes React JSX without red squiggles? Also, is Deno_bindgenI'm super happy to see the Deno NPM compatibility supporting an FFI call using Rust's NAPI crate forKV read countDo `kv.list` and `kv.getAll` count as a read for each value or a single read for the operation?Undefined IP on requestsI'm using **express** and **mongo** on Deno, and i'm also trying to use **express-rate-limit**, howeHow to use `f-partial` from Fresh?Show me an example of using `f-partial` in a Deno Fresh project.KV watch initial valueDoes KV watch always trigger an initial read of the current value? Is there a way to only watch forThe deployment failed: Relative import path "$fresh/server.ts" not prefixed with / or ./ or ../When deploying I get this error: ``` Deploying... (100.0%) Error: The deployment failed: Relative imDeno Deploy: Timeout PageIs there a way to replace the standard deno deploy timeout error page? I have a use case where I allFresh, deno deploy microservices and authHi, I want to design a Fresh frontend and api working with deno microservices and auth. My proposalEventSource vs Multiplex webookI am setting up an system that needs realtime updates for potentially 100+ different resources on a rusty_v8 - how to pass data from js/v8 into rustHello, I am playing with rusty_v8, by following the examples I was able to get js eval up and runninDeno deploy - My simple mail service is giving a connection error (works fine locally or on vercel)As the title states my small email sending API is not working on deno deploy. Maybe there needs to bHow to send data from a child component to its sibling?I want create an app with two forms, each form in its own component. Users type in form 1, then the Can Deno list all functions within a script without explicit registration or annotation?. ## For context, here is how you do it for `globalThis`: ```ts Object.getOwnPropertyNames(globalThiTypescript intelisense between jupyter notebooksI've been trying to run jupyter notebooks with deno, but I've noticed that if I create a variable inhow to compress string into stringHow would i go about compressing a string? i need to compress a string but i need the compressed valFresh - Page title to LayoutHi, I have been browsing around the docs and also being new to JSX,I'm not sure what I look for. WhaWhen I use deno run I get this error: error : Permission denied (os error 13)When I use deno run I get this error: error : Permission denied (os error 13) (for '/Users/apple/LibAxios HttpsProxyAgentHello, I tryed to search but found nothing. I'm using axios with httpProxyAgent to proxy my outgoingGet input from an island, make calculus then render ?I have a route with 2 islands inside - one to get input from user - one to render data Once I've go