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 8116369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:48:34+00:00 2026-06-06T03:48:34+00:00

I have a JavaScript Websockets implementation where I would like to use a singleton

  • 0

I have a JavaScript Websockets implementation where I would like to use a singleton model that uses one Websocket connection for multiple calls to the server but with different callback event handlers. I have the implementation working just fine but have noticed some strange behaviors with messages being directed to the wrong callback handler. Here is some code:

Connection.js file

var connection = function(){
    var _socket = null;
    return {
        socket : function(){
            if (_socket == null){
                _socket = new WebSocket("ws://localhost:8081/index.ashx");
                _socket.onclose = function(evt){alert('Closed');}
                _socket.extraParameter = null;
            }
            return _socket;
        },
        send : function(data, callback){
            var localSocket = connection.socket();
            localSocket.extraParameter = new Date().toString();
            localSocket.onmessage = callback;
            localSocket.originalDataSent = data;
            localSocket.send(data);
        }
    }
}();

App.js file

var App = function(){
    return {
        cpuUtilization : function(evt){
            var localSocket = this;
            var dateTimeOfRequest = localSocket.extraParameter;
            var originalDataSent = localSocket.originalDataSent
            var jsonData = $.parseJSON(evt.data);
            if ($.parseJSON(originalDataSent).type == "cpu"){
                $("#dateTimeContainer").html();
                $("#cpuContainer").html(jsonData.value);
            }
        }
    }
}();

Third Party Signal.js file

var Signal = function(){
    return {
        handlerProcess : function(evt){
            //  Does some third party stuff...
        }
    }
}();

usage

connection.send("{type:'process'}", Signal.handlerProcess);
connection.send("{type:'cpu'}", App.cpuUtilization);
connection.send("{type:'memory'}", Signal.handlerMemory);
connection.send("{type:'harddrive'}", Signal.handlerHardDrive);

Now where I think I am see the problem is when multiple request are made through the same websocket and the message returns. Since this is asynchronous, I have no way of tieing the request to the event callback. My solution uses the options in the handler for reference, but depending on the time it takes for the websocket request to run, the wrong callback handler is being called and process fails. I think it is failing because I am accessing properties from the websocket instance that may be changing between calls.

Is there a way to pass a reference or additional parameters along with the evt parameter? Maybe wrapping this somehow?

  • 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-06T03:48:38+00:00Added an answer on June 6, 2026 at 3:48 am

    I think it is failing because I am accessing properties from the websocket instance that may be changing between calls.

    Yes.

    Since this is asynchronous, I have no way of tieing the request to the event callback.

    No. You can create a closure for the callback function instead of calling using callback directly:

    ... send: function(data, callback){
        var localSocket = connection.socket();
        var extraParameter = new Date().toString();
        localSocket.onmessage = function(evt) {
            callback(evt.data, /* original- */ data, extraParameter);
        };
        localSocket.send(data);
    }
    

    But still, you have a changing onmessage callback handler. That means, an event may be sent to a handler that does not deserve it. Having an asynchronous system, you will need to add a piece of information to the server resonse that indicates which process the data belongs to. The one universal message handler then could resolve that and call the right callback.

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

Sidebar

Related Questions

I have a JavaScript variable that stores a websocket connection like so: var ws
I have build a game using canvas and Javascript, and I would like to
I have javascript function (that uses Raphael) that on click select area. And I
What WebSockets add to raw TCP connection? Why should I use WebSockets? I'd like
I have Javascript updating my URI as below: /index.php?page=list#page=news But I would like to
I have Javascript that opens another window and registers a click handler for all
I have JavaScript that is doing activity periodically. When the user is not looking
I have javascript string variable with var sttr=We prefer questions that can be answered
I have JavaScript form validation functions like so: function validate_name(field) { if (field ==
I have browser client Javascript which opens a WebSocket (using socket.io) to request a

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.