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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:46:01+00:00 2026-06-16T22:46:01+00:00

I am trying to understand how the Javascript client code of socket.io works. Specifically,

  • 0

I am trying to understand how the Javascript client code of socket.io works. Specifically, my goal is to understand, how does it know where the server is located?

The client code I use in my program is very simple – I just link to socket.io.js:

<script src="./socket.io/socket.io.js"></script>

and then:

var socket = io.connect()

And that’s it – the socket automatically connects to the server. But how does it know?

I looked at socket.io.js, and this is what I found (comments trimmed):

var io = ('undefined' === typeof module ? {} : module.exports);
(function() {

/** Copyright(c) 2011 LearnBoost <dev@learnboost.com> * MIT Licensed */

(function (exports, global) {
  var io = exports;
  io.version = '0.9.11';
  io.protocol = 1;
  io.transports = [];
  io.j = [];
  io.sockets = {};


  /**
   * Manages connections to hosts.
   * @param {String} uri
   * @Param {Boolean} force creation of new socket (defaults to false)
   * @api public
   */
  io.connect = function (host, details) {
    var uri = io.util.parseUri(host)
      , uuri
      , socket;

    if (global && global.location) {
      uri.protocol = uri.protocol || global.location.protocol.slice(0, -1);
      uri.host = uri.host || (global.document
        ? global.document.domain : global.location.hostname);
      uri.port = uri.port || global.location.port;
    }

    uuri = io.util.uniqueUri(uri);

    ... 

})('object' === typeof module ? module.exports : (this.io = {}), this);
...
})();

It seems the secret is in the “global” param, but, who exactly sends “global” to this function? (it’s difficult to understand the functions with all the parentheses…)

  • 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-16T22:46:03+00:00Added an answer on June 16, 2026 at 10:46 pm

    the pattern youre looking for is this:

    (function (exports,global) {
       ///content
    }) (<whatever>, this);
    

    this reference is the scope in which this function is executed (this pattern both defines the function and executes it), which happens to be the global scope. for more information about javascript scopes see here – http://tore.darell.no/pages/scope_in_javascript

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

Sidebar

Related Questions

I'm trying to understand javascript scopes, and I want to know if in the
I am trying to understand the Javascript event handling. I like some code to
i am trying to understand following fragment of javascript code <!DOCTYPE html> <html> <body>
I'm trying to understand how to use firebug to debug my Javascript. So I
After trying to understand why client code is not rendered in a page (injected
I have been trying to understand the follwing program. On server side the following
I am trying to understand a third party Javascript code. But am not able
i have been trying to understand how JavaScript can function as server-side language as
I am trying to understand JavaScript a little better and am having trouble creating
I am trying to understand how a single and multidimensional javascript array would appear

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.