Solved

node.js and socket.io help

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now