[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

node.js and socket.io help

Posted on 2016-08-08
1
Medium Priority
?
121 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:Nathan Riley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 9

Accepted Solution

by:
James Bilous earned 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 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