3

How do I receive events from the browser in my node.js code? (e.g: I imagine Mixpanel, kissmetrics, etc do something like this?

thanks

Mike Causer
  • 8,196
  • 2
  • 43
  • 63
donald
  • 23,587
  • 42
  • 142
  • 223

4 Answers4

3

The same way any other web server receives events from the browser: the browser makes an HTTP request to the URL of your server and the server receives that request. Listening for HTTP requests is the "Hello World" example for node.js.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
2

What you're looking for is http://hummingbirdstats.com/ realtime stats 20 times a second.

You should also checkout socket.io if you haven't. Websockets events are faster than HTTP requests.

generalhenry
  • 17,227
  • 4
  • 48
  • 63
  • Can I use socket.io to know when a user clicks a button for example? would that work for that use case as well and not only pageviews? – donald Jul 19 '11 at 19:47
  • You could track clicks with AJAX calls, they're one every couple seconds. With socket.io you can go as far as tracking mouse-movements, sending in data multiple times a seconds so see the mice moving in real time. – generalhenry Jul 19 '11 at 19:55
  • But how do I know that the mice is moving? Or that the user has clicked in the button? Don't I have to add a "onClick()" ? – donald Jul 19 '11 at 20:03
  • Yes, on the client side (using jQuery) it's something like `$('body').mousemove( function (e) { socket.emit('mousemove',{x:e.pageX,y:e.pageY}); } );` – generalhenry Jul 19 '11 at 20:06
  • AJAX example: `$('button').click(function(){ $.post('/buttonClick',{...}); });` – generalhenry Jul 19 '11 at 20:12
1

You have to send them to your server, via AJAX or some similar method.

Remember, node code runs on the server; the browser runs on the client. The way to get information back and forth from a server to a client running a web browser is an HTTP request.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
1

I recommend dnode. Search for dnode on the browser in the README. It's a quick and complete example of making a RPC. In this case the remote function would be an event handler.

It uses socket.io, which supports websockets, flash sockets, and xhr.

Benjamin Atkin
  • 14,071
  • 7
  • 61
  • 60