Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Socket.io Client Request Origin URL

Socket.io Client Request Origin URL

Problem

On the client, in the browser, I have this code:

this.socket.on('initial', function(data) {
    console.log(data);
});

On the server I have:

socket.sockets.on('connection', function(client){
    console.log('client connected');
});

My question is: how can I detect the URL where the request came from? For example if the first closure was running on "/posts/view/1", I want to be able to detect it inside of the second closure.

Problem courtesy of: Vasily

Solution

You could send this data back to the server. A little hand-wavey on the details, but how about something like:

On the client:

this.socket.on('initial', function(data) {
  // do whatever with data
  var my_location = get_page_location_with_javascript(); // or whatever
  this.socket.emit('phone_home', my_location);
});

On the server:

this.sockets.on('phone_home', function(url) {
  console.log("The URL was " + url);
});
Solution courtesy of: Michelle Tilley

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Socket.io Client Request Origin URL

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×