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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:20:42+00:00 2026-05-24T22:20:42+00:00

Is there any globals function in Javascript which is similar to PHP globals that

  • 0

Is there any “globals” function in Javascript which is similar to PHP globals that would apply in this example?

var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
querystring = require('querystring');

app.listen(8000);

var content = '';

function handler(req, res) {
    io.sockets.on('connection', function (socket) {
        if(req.method == 'POST') {
            var fullBody = '';

            req.on('data', function(chunk) {
                fullBody += chunk.toString();
            });

            req.on('end', function() {
                var decodedBody = querystring.parse(fullBody);
                console.log(decodedBody);

                socket.emit('user-aaa5c8bbffe4db9', fullBody);
            });
        }
    });

    fs.readFile(__dirname + '/index.html',
    function(err, data) {
        if(err) {
            res.writeHead(500);
            return res.end('Error loading index.html');
        }

        res.writeHead(200);
        res.end(data);
    });
}

I wrote a PHP app but I want to have notifications through socket.io but I don’t want to rewrite the app for Node.js (yet) so I setup a Node server which the PHP will send a POST to and then the server will send the notification via Socket.io. I’ve tested the server with basic scripts and they do work. I’ve also tried moving the “io.sockets.on(‘connection’, function (socket) {” inside the IF statement:

req.on('end', function() {
    var decodedBody = querystring.parse(fullBody);
    console.log(decodedBody);

    io.sockets.on('connection', function (socket) {
        socket.emit('user-aaa5c8bbffe4db9', fullBody);
    });
});

But that did not produce “instant” results, the notifications would come in to the client after a page refresh.

Client is a simple:

socket.on('user-<?php echo $_SESSION['user_display_id']; ?>', function (data) {
    alert(data);
    $('#events').html(data);
});

Any help would be greatly appreciated

  • 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-24T22:20:44+00:00Added an answer on May 24, 2026 at 10:20 pm

    To answer your first question. No there is no global in JavaScript. All the variables that are in scope where the function is defined will be available in that function.

    http://javascriptweblog.wordpress.com/2010/10/25/understanding-javascript-closures/

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

Sidebar

Related Questions

I know that there are several ways to define a function in JavaScript. Two
Is there any way of writing a decorator such that the following would work?
are there any libraries for Linux wrote with C++, that could register global hotkeys
In Ruby is there any method that lists all the global variables available at
Is there any free or commercial component written in .NET (no COM interop) that
Is there any query which can return me the number of revisions made to
Is there any efficiency difference in an explicit vs implicit inner join? For example:
I have a javascript function that, in most cases, needs to do something with
My intuitive response to this question would be ,This is so stupid that I
I found this file at google code with the function: function SetAlwaysOnTop() { var

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.