Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:10:45+00:00 2026-06-09T22:10:45+00:00

I’m using the ws module in nodejs for a web socket server, and the

  • 0

I’m using the ws module in nodejs for a web socket server, and the simplified version of the code is this:

var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({port: 9001});

wss.on('connection', function (ws) {

    // is the ws object created here ?

  ws.on('message', function (message) {
    if (message[0] === '+') {
        ws.name = message.substring(1);
        console.log(ws.name+' connected');
    }
  });

  ws.on('close', function () {
    console.log(ws.name+' disconnected');
    // Will this ws object be deleted ?
  });
});

I would like to know:

  • On each event of a connection, is a ws object created ?
  • Does one of this object per connected client persist in RAM until it’s garbage collected ?

And most importantly:
If tons of clients disconnect and the ws objects keep lying around until they are garbage collected, when the garbage collection happens, my server could be locked for quite some time right ? Should I go through the trouble of storing these ws objects in another object, so I could use the delete keyword and remove them as soon as I get the close event ?

If I were to do so, my could would be somewhat like this:

var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({port: 9001});

var websockets = {};

wss.on('connection', function (ws) {
  ws.on('message', function (message) {
    if (message[0] === '+') {
        ws.name = message.substring(1);
        websockets[ws.name] = ws; // Add to the object that stores ws objects
        console.log(ws.name+' connected');
    }
  });

  ws.on('close', function () {
    delete websockets[ws.name]; // Delete from the object
    console.log(ws.name+' disconnected');
  });
});

So, is it worth it ? Am I writing more garbage collection friendly code in the second snippet ? Will it help avoid locking up for a long time when a garbage collection happens ?

UPDATE: I’m sorry I just realised how stupid the second snippet of code is. I’m actually duplicating the amount of objects when I do websockets[ws.name] = ws… But the first part of the question still remains valid.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:10:47+00:00Added an answer on June 9, 2026 at 10:10 pm

    On each event of a connection, is a ws object created ?

    Yes.

    Does one of this object per connected client persist in RAM until it’s garbage collected ?

    Yes, that’s true for every JavaScript object.

    Should I go through the trouble of storing these ws objects in another object, so I could use the delete keyword and remove them as soon as I get the close event ?

    No. Using delete keyword won’t free your memory. In fact it does nothing else then just removing the reference. Consider this example:

    var x = { };
    var y = { };
    var z = { };
    x.test = z;
    y.test = z;
    delete x.test;
    

    As you can see x.test is removed ( the object x no longer has .test attribute ) but z is not deleted at all, because y holds reference to z.

    And how do you think WS can work with multiple ws objects? It stores them somewhere internally. If you want to remove ws object, then simply do

    ws.on('close', function () {
        delete ws;
    });
    

    This will ensure that at some point the garbage collector will collect ws. On the other hand I think that WS already takes care of this internally, so you don’t have to worry about it at all.

    Side note: There is no way to trigger garbage collector in JavaScript. But do not worry about it. If you reach your memory limit it will fire on its own.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.