Assigning WebSocket and net.Socket with unique idIgnore Typescript Errors “property does not exist on value of type”webSocketServer node.js how to differentiate clientsHow to uniquely identify a socket with Node.jsNodeJS WebSocket Handshake Silently Failing?WebSockets vs. Server-Sent events/EventSourceDifferences between socket.io and websocketsWhat are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?Which websocket library to use with Node.js?Which WebSocket library to use in Android app?How to pass an active WebSocket to a clustered thread in Node.js?Why does not sending data io.sockets.emit and socket.broadcast.emitReact WebSocket integration sockjs react-websocket socketio-clientListen to Websockets in Angular 4 app with Socket.io

Giant Toughroad SLR 2 for 200 miles in two days, will it make it?

Why are on-board computers allowed to change controls without notifying the pilots?

Bob has never been a M before

Can a Gentile theist be saved?

Meta programming: Declare a new struct on the fly

Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?

How will losing mobility of one hand affect my career as a programmer?

Are Warlocks Arcane or Divine?

How can I successfully establish a nationwide combat training program for a large country?

What would you call a finite collection of unordered objects that are not necessarily distinct?

How to deal with or prevent idle in the test team?

Could solar power be utilized and substitute coal in the 19th century?

Stereotypical names

Can the electrostatic force be infinite in magnitude?

Did US corporations pay demonstrators in the German demonstrations against article 13?

How do I repair my stair bannister?

How to prevent YouTube from showing already watched videos?

Freedom of speech and where it applies

Is a naturally all "male" species possible?

Pronouncing Homer as in modern Greek

For airliners, what prevents wing strikes on landing in bad weather?

Is there an wasy way to program in Tikz something like the one in the image?

Superhero words!

Is it okay / does it make sense for another player to join a running game of Munchkin?



Assigning WebSocket and net.Socket with unique id


Ignore Typescript Errors “property does not exist on value of type”webSocketServer node.js how to differentiate clientsHow to uniquely identify a socket with Node.jsNodeJS WebSocket Handshake Silently Failing?WebSockets vs. Server-Sent events/EventSourceDifferences between socket.io and websocketsWhat are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?Which websocket library to use with Node.js?Which WebSocket library to use in Android app?How to pass an active WebSocket to a clustered thread in Node.js?Why does not sending data io.sockets.emit and socket.broadcast.emitReact WebSocket integration sockjs react-websocket socketio-clientListen to Websockets in Angular 4 app with Socket.io













4















I aspire to assign Websockets and net.Sockets with unique identifiers, so when a message is received, the client is identified by the identifier attached to the socket.



Previous research:



For Websocket:



According to this and this, the following is requires:



const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
ws.id = uuid.v4(); // This is the relevant line of code
ws.on('message', (msg: string) =>
...

);


For net.Socket:



Quite the same - according to this, the following is required:



var server = net.createServer();

server.on('connection', function(conn)
conn.id = uuid.v4(); // This is the relevant line of code
conn.on('data', function(data)
...
);
);


The problem



The error "Property 'id' does not exist on type 'WebSocket' [or 'Socket' accordingly]" during compilation. This explains why.



Optional Solution #1: Cast to any



The update net.Socket code will be:



var server = net.createServer();

server.on('connection', function(conn)
(conn as any).id = uuid.v4();
conn.on('data', function(data)
console.log('Session id:' + (conn as any).id);
);
);


The problem: It doesn't seem like good practice to me. I don't have references for why except for a hunch for now.



Optional Solution #2: Use a local variable [seems like a better option]



The update net.Socket code will be:



var server = net.createServer();

server.on('connection', function(conn)
const id: string = uuid.v4();
conn.on('data', function(data)
console.log('Session id:' + id);
);
);


After some experiencing, it seems to work properly.



So - the questions:




  1. In optional solution #2: is it guaranteed that the id variable will always be available in this scope? In other words: is this solution valid? for both net.Socket & Websocket? Or is there something that I'm missing?


  2. In general: Is there additional identifiers (perhaps built in identifiers in WebSocket and net.Socket) that can be used instead?

[Typescript version: 3.2.4].










share|improve this question



















  • 1





    for 1, it's valid solution, check this developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

    – Tubc
    Mar 9 at 10:56















4















I aspire to assign Websockets and net.Sockets with unique identifiers, so when a message is received, the client is identified by the identifier attached to the socket.



Previous research:



For Websocket:



According to this and this, the following is requires:



const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
ws.id = uuid.v4(); // This is the relevant line of code
ws.on('message', (msg: string) =>
...

);


For net.Socket:



Quite the same - according to this, the following is required:



var server = net.createServer();

server.on('connection', function(conn)
conn.id = uuid.v4(); // This is the relevant line of code
conn.on('data', function(data)
...
);
);


The problem



The error "Property 'id' does not exist on type 'WebSocket' [or 'Socket' accordingly]" during compilation. This explains why.



Optional Solution #1: Cast to any



The update net.Socket code will be:



var server = net.createServer();

server.on('connection', function(conn)
(conn as any).id = uuid.v4();
conn.on('data', function(data)
console.log('Session id:' + (conn as any).id);
);
);


The problem: It doesn't seem like good practice to me. I don't have references for why except for a hunch for now.



Optional Solution #2: Use a local variable [seems like a better option]



The update net.Socket code will be:



var server = net.createServer();

server.on('connection', function(conn)
const id: string = uuid.v4();
conn.on('data', function(data)
console.log('Session id:' + id);
);
);


After some experiencing, it seems to work properly.



So - the questions:




  1. In optional solution #2: is it guaranteed that the id variable will always be available in this scope? In other words: is this solution valid? for both net.Socket & Websocket? Or is there something that I'm missing?


  2. In general: Is there additional identifiers (perhaps built in identifiers in WebSocket and net.Socket) that can be used instead?

[Typescript version: 3.2.4].










share|improve this question



















  • 1





    for 1, it's valid solution, check this developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

    – Tubc
    Mar 9 at 10:56













4












4








4








I aspire to assign Websockets and net.Sockets with unique identifiers, so when a message is received, the client is identified by the identifier attached to the socket.



Previous research:



For Websocket:



According to this and this, the following is requires:



const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
ws.id = uuid.v4(); // This is the relevant line of code
ws.on('message', (msg: string) =>
...

);


For net.Socket:



Quite the same - according to this, the following is required:



var server = net.createServer();

server.on('connection', function(conn)
conn.id = uuid.v4(); // This is the relevant line of code
conn.on('data', function(data)
...
);
);


The problem



The error "Property 'id' does not exist on type 'WebSocket' [or 'Socket' accordingly]" during compilation. This explains why.



Optional Solution #1: Cast to any



The update net.Socket code will be:



var server = net.createServer();

server.on('connection', function(conn)
(conn as any).id = uuid.v4();
conn.on('data', function(data)
console.log('Session id:' + (conn as any).id);
);
);


The problem: It doesn't seem like good practice to me. I don't have references for why except for a hunch for now.



Optional Solution #2: Use a local variable [seems like a better option]



The update net.Socket code will be:



var server = net.createServer();

server.on('connection', function(conn)
const id: string = uuid.v4();
conn.on('data', function(data)
console.log('Session id:' + id);
);
);


After some experiencing, it seems to work properly.



So - the questions:




  1. In optional solution #2: is it guaranteed that the id variable will always be available in this scope? In other words: is this solution valid? for both net.Socket & Websocket? Or is there something that I'm missing?


  2. In general: Is there additional identifiers (perhaps built in identifiers in WebSocket and net.Socket) that can be used instead?

[Typescript version: 3.2.4].










share|improve this question
















I aspire to assign Websockets and net.Sockets with unique identifiers, so when a message is received, the client is identified by the identifier attached to the socket.



Previous research:



For Websocket:



According to this and this, the following is requires:



const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
ws.id = uuid.v4(); // This is the relevant line of code
ws.on('message', (msg: string) =>
...

);


For net.Socket:



Quite the same - according to this, the following is required:



var server = net.createServer();

server.on('connection', function(conn)
conn.id = uuid.v4(); // This is the relevant line of code
conn.on('data', function(data)
...
);
);


The problem



The error "Property 'id' does not exist on type 'WebSocket' [or 'Socket' accordingly]" during compilation. This explains why.



Optional Solution #1: Cast to any



The update net.Socket code will be:



var server = net.createServer();

server.on('connection', function(conn)
(conn as any).id = uuid.v4();
conn.on('data', function(data)
console.log('Session id:' + (conn as any).id);
);
);


The problem: It doesn't seem like good practice to me. I don't have references for why except for a hunch for now.



Optional Solution #2: Use a local variable [seems like a better option]



The update net.Socket code will be:



var server = net.createServer();

server.on('connection', function(conn)
const id: string = uuid.v4();
conn.on('data', function(data)
console.log('Session id:' + id);
);
);


After some experiencing, it seems to work properly.



So - the questions:




  1. In optional solution #2: is it guaranteed that the id variable will always be available in this scope? In other words: is this solution valid? for both net.Socket & Websocket? Or is there something that I'm missing?


  2. In general: Is there additional identifiers (perhaps built in identifiers in WebSocket and net.Socket) that can be used instead?

[Typescript version: 3.2.4].







node.js typescript sockets websocket






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 14:40









Louis

97.8k22188238




97.8k22188238










asked Mar 7 at 9:59









MaoritzioMaoritzio

5202623




5202623







  • 1





    for 1, it's valid solution, check this developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

    – Tubc
    Mar 9 at 10:56












  • 1





    for 1, it's valid solution, check this developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

    – Tubc
    Mar 9 at 10:56







1




1





for 1, it's valid solution, check this developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

– Tubc
Mar 9 at 10:56





for 1, it's valid solution, check this developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

– Tubc
Mar 9 at 10:56












1 Answer
1






active

oldest

votes


















4





+50









I'm not aware of a field that already exists for that.



If you can keep the socket id in a local variable like you do in option 2, that's what I'd do. I generally prefer to avoid adding arbitrary fields to objects. However, sometimes you want the entirety of your application to be able to access the additional piece of data, and in those cases using a local variable won't work.



Adding a custom field



Instead of using a type assertion to any, you could add a file to your project that contains this:



declare module "net" 
interface Socket
id: string;




This is augmenting the net.Socket interface to add an id field which is a string. This is a bit neater than a type assertion because the type assertion would let typos go through (e.g. (conn as any).ids). I used your code, removed the type assertion and added the above in a file named externals.d.ts that I put next to the .ts file that contains your code. tsc stopped complaining about the field. Note that you don't need to import this file or refer to it in any way. You must just have a tsconfig.json that picks it up along with the rest of your source. By default, it would be picked up due to the .d.ts extension.



In the past I've used type assertions and interface augmentation to add arbitrary fields to DOM nodes, and it worked just fine. However, when I did that, I used field names that were very singular, meaning that there was a very low chance of a clash with other libraries that might want to add their own fields. Your field name is id. I'd be worried about name clashes with other libraries that decide they want to keep track of sockets and add their own id field to a socket.



Using WeakMap



There's another method you can use. You could setup a WeakMap that associates the socket with the id. Here's an illustration. You could have a module socket-map that just exports a map that maps sockets to strings:



import * as net from "net";

export const socketMap = new WeakMap<net.Socket, string>();


And then you'd store the socket with the id when you obtain the socket:



import * as net from "net";
import * as uuid from "uuid";
import socketMap from "./socket-map";

var server = net.createServer();

server.on('connection', function(conn)
const id = uuid.v4();
socketMap.set(conn, id); // You store the socket into the map.
conn.on('data', function(data)
console.log('Session id:' + (conn as any).id);
);
);


Then later, in another module, you could get the id back with:



import * as net from "net";
import socketMap from "./socket-map";

export function foo(conn: net.Socket)
const id = socketMap.get(conn);



Here I've just associated the socket with an id string, but you could have any structure you want in the values of the WeakMap. It could be an object that contains a whole slew of information besides an id.



The reason to use WeakMap is while the keys of a WeakMap object contain references to objects, these references do not count as far as garbage collection goes. So if your application is done with a socket and no longer references it anywhere than a WeakMap, the reference present in the WeakMap will still allow the socket to be collected by the garbage collector.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55040899%2fassigning-websocket-and-net-socket-with-unique-id%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4





    +50









    I'm not aware of a field that already exists for that.



    If you can keep the socket id in a local variable like you do in option 2, that's what I'd do. I generally prefer to avoid adding arbitrary fields to objects. However, sometimes you want the entirety of your application to be able to access the additional piece of data, and in those cases using a local variable won't work.



    Adding a custom field



    Instead of using a type assertion to any, you could add a file to your project that contains this:



    declare module "net" 
    interface Socket
    id: string;




    This is augmenting the net.Socket interface to add an id field which is a string. This is a bit neater than a type assertion because the type assertion would let typos go through (e.g. (conn as any).ids). I used your code, removed the type assertion and added the above in a file named externals.d.ts that I put next to the .ts file that contains your code. tsc stopped complaining about the field. Note that you don't need to import this file or refer to it in any way. You must just have a tsconfig.json that picks it up along with the rest of your source. By default, it would be picked up due to the .d.ts extension.



    In the past I've used type assertions and interface augmentation to add arbitrary fields to DOM nodes, and it worked just fine. However, when I did that, I used field names that were very singular, meaning that there was a very low chance of a clash with other libraries that might want to add their own fields. Your field name is id. I'd be worried about name clashes with other libraries that decide they want to keep track of sockets and add their own id field to a socket.



    Using WeakMap



    There's another method you can use. You could setup a WeakMap that associates the socket with the id. Here's an illustration. You could have a module socket-map that just exports a map that maps sockets to strings:



    import * as net from "net";

    export const socketMap = new WeakMap<net.Socket, string>();


    And then you'd store the socket with the id when you obtain the socket:



    import * as net from "net";
    import * as uuid from "uuid";
    import socketMap from "./socket-map";

    var server = net.createServer();

    server.on('connection', function(conn)
    const id = uuid.v4();
    socketMap.set(conn, id); // You store the socket into the map.
    conn.on('data', function(data)
    console.log('Session id:' + (conn as any).id);
    );
    );


    Then later, in another module, you could get the id back with:



    import * as net from "net";
    import socketMap from "./socket-map";

    export function foo(conn: net.Socket)
    const id = socketMap.get(conn);



    Here I've just associated the socket with an id string, but you could have any structure you want in the values of the WeakMap. It could be an object that contains a whole slew of information besides an id.



    The reason to use WeakMap is while the keys of a WeakMap object contain references to objects, these references do not count as far as garbage collection goes. So if your application is done with a socket and no longer references it anywhere than a WeakMap, the reference present in the WeakMap will still allow the socket to be collected by the garbage collector.






    share|improve this answer



























      4





      +50









      I'm not aware of a field that already exists for that.



      If you can keep the socket id in a local variable like you do in option 2, that's what I'd do. I generally prefer to avoid adding arbitrary fields to objects. However, sometimes you want the entirety of your application to be able to access the additional piece of data, and in those cases using a local variable won't work.



      Adding a custom field



      Instead of using a type assertion to any, you could add a file to your project that contains this:



      declare module "net" 
      interface Socket
      id: string;




      This is augmenting the net.Socket interface to add an id field which is a string. This is a bit neater than a type assertion because the type assertion would let typos go through (e.g. (conn as any).ids). I used your code, removed the type assertion and added the above in a file named externals.d.ts that I put next to the .ts file that contains your code. tsc stopped complaining about the field. Note that you don't need to import this file or refer to it in any way. You must just have a tsconfig.json that picks it up along with the rest of your source. By default, it would be picked up due to the .d.ts extension.



      In the past I've used type assertions and interface augmentation to add arbitrary fields to DOM nodes, and it worked just fine. However, when I did that, I used field names that were very singular, meaning that there was a very low chance of a clash with other libraries that might want to add their own fields. Your field name is id. I'd be worried about name clashes with other libraries that decide they want to keep track of sockets and add their own id field to a socket.



      Using WeakMap



      There's another method you can use. You could setup a WeakMap that associates the socket with the id. Here's an illustration. You could have a module socket-map that just exports a map that maps sockets to strings:



      import * as net from "net";

      export const socketMap = new WeakMap<net.Socket, string>();


      And then you'd store the socket with the id when you obtain the socket:



      import * as net from "net";
      import * as uuid from "uuid";
      import socketMap from "./socket-map";

      var server = net.createServer();

      server.on('connection', function(conn)
      const id = uuid.v4();
      socketMap.set(conn, id); // You store the socket into the map.
      conn.on('data', function(data)
      console.log('Session id:' + (conn as any).id);
      );
      );


      Then later, in another module, you could get the id back with:



      import * as net from "net";
      import socketMap from "./socket-map";

      export function foo(conn: net.Socket)
      const id = socketMap.get(conn);



      Here I've just associated the socket with an id string, but you could have any structure you want in the values of the WeakMap. It could be an object that contains a whole slew of information besides an id.



      The reason to use WeakMap is while the keys of a WeakMap object contain references to objects, these references do not count as far as garbage collection goes. So if your application is done with a socket and no longer references it anywhere than a WeakMap, the reference present in the WeakMap will still allow the socket to be collected by the garbage collector.






      share|improve this answer

























        4





        +50







        4





        +50



        4




        +50





        I'm not aware of a field that already exists for that.



        If you can keep the socket id in a local variable like you do in option 2, that's what I'd do. I generally prefer to avoid adding arbitrary fields to objects. However, sometimes you want the entirety of your application to be able to access the additional piece of data, and in those cases using a local variable won't work.



        Adding a custom field



        Instead of using a type assertion to any, you could add a file to your project that contains this:



        declare module "net" 
        interface Socket
        id: string;




        This is augmenting the net.Socket interface to add an id field which is a string. This is a bit neater than a type assertion because the type assertion would let typos go through (e.g. (conn as any).ids). I used your code, removed the type assertion and added the above in a file named externals.d.ts that I put next to the .ts file that contains your code. tsc stopped complaining about the field. Note that you don't need to import this file or refer to it in any way. You must just have a tsconfig.json that picks it up along with the rest of your source. By default, it would be picked up due to the .d.ts extension.



        In the past I've used type assertions and interface augmentation to add arbitrary fields to DOM nodes, and it worked just fine. However, when I did that, I used field names that were very singular, meaning that there was a very low chance of a clash with other libraries that might want to add their own fields. Your field name is id. I'd be worried about name clashes with other libraries that decide they want to keep track of sockets and add their own id field to a socket.



        Using WeakMap



        There's another method you can use. You could setup a WeakMap that associates the socket with the id. Here's an illustration. You could have a module socket-map that just exports a map that maps sockets to strings:



        import * as net from "net";

        export const socketMap = new WeakMap<net.Socket, string>();


        And then you'd store the socket with the id when you obtain the socket:



        import * as net from "net";
        import * as uuid from "uuid";
        import socketMap from "./socket-map";

        var server = net.createServer();

        server.on('connection', function(conn)
        const id = uuid.v4();
        socketMap.set(conn, id); // You store the socket into the map.
        conn.on('data', function(data)
        console.log('Session id:' + (conn as any).id);
        );
        );


        Then later, in another module, you could get the id back with:



        import * as net from "net";
        import socketMap from "./socket-map";

        export function foo(conn: net.Socket)
        const id = socketMap.get(conn);



        Here I've just associated the socket with an id string, but you could have any structure you want in the values of the WeakMap. It could be an object that contains a whole slew of information besides an id.



        The reason to use WeakMap is while the keys of a WeakMap object contain references to objects, these references do not count as far as garbage collection goes. So if your application is done with a socket and no longer references it anywhere than a WeakMap, the reference present in the WeakMap will still allow the socket to be collected by the garbage collector.






        share|improve this answer













        I'm not aware of a field that already exists for that.



        If you can keep the socket id in a local variable like you do in option 2, that's what I'd do. I generally prefer to avoid adding arbitrary fields to objects. However, sometimes you want the entirety of your application to be able to access the additional piece of data, and in those cases using a local variable won't work.



        Adding a custom field



        Instead of using a type assertion to any, you could add a file to your project that contains this:



        declare module "net" 
        interface Socket
        id: string;




        This is augmenting the net.Socket interface to add an id field which is a string. This is a bit neater than a type assertion because the type assertion would let typos go through (e.g. (conn as any).ids). I used your code, removed the type assertion and added the above in a file named externals.d.ts that I put next to the .ts file that contains your code. tsc stopped complaining about the field. Note that you don't need to import this file or refer to it in any way. You must just have a tsconfig.json that picks it up along with the rest of your source. By default, it would be picked up due to the .d.ts extension.



        In the past I've used type assertions and interface augmentation to add arbitrary fields to DOM nodes, and it worked just fine. However, when I did that, I used field names that were very singular, meaning that there was a very low chance of a clash with other libraries that might want to add their own fields. Your field name is id. I'd be worried about name clashes with other libraries that decide they want to keep track of sockets and add their own id field to a socket.



        Using WeakMap



        There's another method you can use. You could setup a WeakMap that associates the socket with the id. Here's an illustration. You could have a module socket-map that just exports a map that maps sockets to strings:



        import * as net from "net";

        export const socketMap = new WeakMap<net.Socket, string>();


        And then you'd store the socket with the id when you obtain the socket:



        import * as net from "net";
        import * as uuid from "uuid";
        import socketMap from "./socket-map";

        var server = net.createServer();

        server.on('connection', function(conn)
        const id = uuid.v4();
        socketMap.set(conn, id); // You store the socket into the map.
        conn.on('data', function(data)
        console.log('Session id:' + (conn as any).id);
        );
        );


        Then later, in another module, you could get the id back with:



        import * as net from "net";
        import socketMap from "./socket-map";

        export function foo(conn: net.Socket)
        const id = socketMap.get(conn);



        Here I've just associated the socket with an id string, but you could have any structure you want in the values of the WeakMap. It could be an object that contains a whole slew of information besides an id.



        The reason to use WeakMap is while the keys of a WeakMap object contain references to objects, these references do not count as far as garbage collection goes. So if your application is done with a socket and no longer references it anywhere than a WeakMap, the reference present in the WeakMap will still allow the socket to be collected by the garbage collector.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 14:31









        LouisLouis

        97.8k22188238




        97.8k22188238





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55040899%2fassigning-websocket-and-net-socket-with-unique-id%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович