Solved

node.js and socket.io help

Posted on 2016-08-08
1
79 Views
Last Modified: 2016-08-08
I'm trying to figure out how to pass custom variables from the one script to the other.

So my script that users hit:
<html>
<body>
<h1>Simple Page</h1>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
    var socket = io.connect('http://node.domain.com:8000', {orgID:'abc'});
    socket.on('connect', function () {

      socket.send(window.location);
    });
</script>
</body>
</html>

Open in new window


You will see I'm trying to pass a custom orgID.  Not sure if this is correct.

Then my server.js looks like this, how do I set it into a variable there? Right now it just shows undefined for it in console.

var io = require('socket.io').listen(8000);
var mysql      = require('mysql');
var con = mysql.createConnection({
    host     : 'localhost',
    user     : 'user',
    password : 'asdf_user',
    database : 'asdf_dev'
});

con.connect(function(err){
    if(err){
        console.log('Error connecting to Db');
        return;
    }
    console.log('Connection established');
});

io.sockets.on('connection', function (socket) {

    socket.on('message', function (message) {
        console.log("Got message: " + message);
        console.log(socket.handshake.orgID);
        var ip = socket.handshake.address;
        url = message;
        //io.sockets.emit('pageview', { 'url': message, 'ip': ip });
        io.sockets.emit('pageview', { 'url': url, 'timestamp': new Date(), 'ip': ip});


        var post  = {pageName: url.pathname, ip: ip};
        con.query('INSERT INTO analytics SET ?', post, function(err, res) {
            if(err) throw err;
        });
    });
});

Open in new window

0
Comment
Question by:N R
1 Comment
 
LVL 8

Accepted Solution

by:
James Bilous earned 500 total points
ID: 41747988
Sending:

    var socket = io.connect('http://node.domain.com:8000', {payload:"orgID=abc"});
    socket.on('connect', function () {

      socket.send(window.location);
    });

Open in new window


Receiving:

io.sockets.on('connection', function (socket) {
   var orgId = socket.request._payload['orgID'];
});

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question