Solved

node.js and socket.io help

Posted on 2016-08-08
1
91 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

733 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