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

  • Home
  • SEARCH
  • 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 6739129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:27:02+00:00 2026-05-26T11:27:02+00:00

I am writing a simple WebSocket application with socketio . There are 2 socket

  • 0

I am writing a simple WebSocket application with socketio. There are 2 socket events that I can implement, on connect and on message. The first one takes the socket as parameter(client) and second takes the data sent from client. So I could write it this way to use the socket variable in on message event:

WebSocketNoaListener.onClientConnect = function(socket){
var soc = socket;
socket.on("message", function(msg){
         console.log("i can use message: "+msg);
         console.log("and the socket: "+soc.id);
    });
socket.on("disconnect", WebSocketNoaListener.onClientDisconnect);
}

But i want to separate my functions for better readability:

WebSocketNoaListener.onClientConnect = function(socket){
var soc = socket;
socket.on("message", WebSocketNoaListener.onClientMessage);
socket.on("disconnect", WebSocketNoaListener.onClientDisconnect);
}


WebSocketNoaListener.onClientMessage = function(){...

I would need to use the soc variable in a scope outside the function that receives socket. Is that possible?

  • 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-05-26T11:27:03+00:00Added an answer on May 26, 2026 at 11:27 am

    You should really do something like this instead of polluting the global space (moreover, the solution given in the two other answers wont be able to handle two simultaneous connections, because soc is overridden at each connection).

    WebSocketNoaListener.onClientMessage = function(soc, msg){
      console.log("i can use message: " + msg);
      console.log("and the socket: " + soc.id);
    };
    WebSocketNoaListener.onClientDisconnect = function(soc, event){
      console.log("not implemented in question");
    };
    WebSocketNoaListener.onClientConnect = function(socket){
      socket.on("message", WebSocketNoaListener.onClientMessage.bind(this, socket));
      socket.on("disconnect", WebSocketNoaListener.onClientDisconnect.bind(this, socket));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I writing some simple application that will look for some message ( sms message
I'm writing a simple OpenGL application that uses GLUT . I don't want to
I was writing a simple test application. There are two radio buttons within the
Am writing a simple application which can write a to pdf,doc,xls and access files.
I am writing a simple threading application. Thread is simply a message consumer and
I'm writing a simple tooltip that can hold HTML tags. Please check http://jsfiddle.net/Qkwm8/ for
I am writing simple site that requires users and profiles to be handled. The
I'm writing a simple app that's going to have a tiny form sitting in
I am currently writing a simple, timer-based mini app in C# that performs an
I'm writing simple server/client in c, where server temporary stores message from client and

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.