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

The Archive Base Latest Questions

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

I’m configuring a nodejs server in order to use it as a server for

  • 0

I’m configuring a nodejs server in order to use it as a server for push notifications.

I have this code:

io.sockets.on('connection', function(socket) {
    console.log(socket);
    // watching the xml file
    fs.watchFile('client.xml', function(curr, prev) {
        // on file change we can read the new xml
        fs.readFile('client.xml', function(err, data) {
            if (err)
                throw err;
            // parsing the new xml datas and converting them into json file
            parser.parseString(data);
        });
    });
    // when the parser ends the parsing we are ready to send the new data to the
    // frontend page
    parser.addListener('end', function(result) {
        socket.volatile.emit('notification', result);
    });
});

On server. And on client this one:

<script>
    function test(data) {
        console.log(data);
        jQuery('#' + data.id).html(data.content);
    }
</script>

<script>
    // creating a new websocket
    var socket = io.connect('http://10.0.0.113:8000');
    // on every message recived we print the new datas inside the #container div
    socket.emit('data', {id : 'id'});
    socket.set('nickname', {id : 'test'});
    socket.on('notification', function(data) {
        _efbn(data.callback, window, data.response, data);
    });

    /**
     * Función que ejecuta una función por nombre. Puede usar namespaces
     * (algo.algo.algo.funct)
     * 
     * @see http://stackoverflow.com/questions/359788/javascript-function-name-as-a-string/359910#359910
     */
    function _efbn(functionName, context) {
        var args = Array.prototype.slice.call(arguments);
        args = [ args[2], args[3] ]; // Fix para IE.
        var namespaces = functionName.split(".");
        var func = namespaces.pop();
        for ( var i = 0; i < namespaces.length; i++) {
            context = context[namespaces[i]];
        }
        try {
            if (typeof context[func] == 'function') {
                return context[func].apply(this, args);
            }
        } catch (e) {
            console.log(e);
        }
        return null;
    }
</script>

Everything is working as I wish. However, I want to send something like:

socket.set('site', '12345');
socket.set('object', 'ABCDE');

In order to identify which xml should I be listening, instead of being listening allways to client.xml.

How can I do it? I have this on server.js:

id = require('url').parse(req.url, true).query.id;

But, since server its only executed when the connection is open, but no when the socket is open, if the connection between the client and the server fails, when jquery retries to open the socket, id will be null…

  • 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:59:42+00:00Added an answer on June 9, 2026 at 10:59 pm

    I would do this instead on the server,

    io.sockets.on('connection', function(socket) {
        console.log(socket);
        socket.on('setup', function(config) {
            // use your connection specific config variables like
            var id = config.id; // and then use id in your logic below.
            // watching the xml file
            fs.watchFile('client.xml', function(curr, prev) {
                // on file change we can read the new xml
                fs.readFile('client.xml', function(err, data) {
                    if (err)
                        throw err;
                    // parsing the new xml datas and converting them into json file
                    parser.parseString(data);
                });
            });
            // when the parser ends the parsing we are ready to send the new data to the
            // frontend page
            parser.addListener('end', function(result) {
                socket.volatile.emit('notification', result);
            });
        });
    });
    

    And on the client-side,

    // creating a new websocket
    var socket = io.connect('http://10.0.0.113:8000');
    socket.on("connect", function() {
        // Setup your connection on the server-side by providing it
        // some config variables.
        var config = {
            id: "id",
            nickname: "test"
        };
        socket.emit("setup", config);
        // on every message recived we print the new datas inside the #container div
        socket.on('notification', function(data) {
            _efbn(data.callback, window, data.response, data);
        });
    });
    

    This way, whenever a new connection is established, the setup event is first called on the server with the required config variables.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.