1

The main reason that I've begun learning Node.js is the idea of having the server push data to the client rather than having the client constantly query the server for any updates. Surely this is possible (thinking about IM web services, etc), but so far in my studies of node, I haven't figured out how it's done.

What method is used? I'm assuming AJAX/JSON is the medium, but what's the method to actually send the data to the webpage, specifically doing so with a node script?

  • Look up [Web Sockets](https://developer.mozilla.org/en-US/docs/WebSockets) and [long polling](http://stackoverflow.com/questions/10028770/html5-websocket-vs-long-polling-vs-ajax-vs-webrtc). – jfriend00 Feb 28 '14 at 21:26

2 Answers2

1

Guess you're aware of socket.io. Very simple to integrate with node, I too started few days before.

I'm not including the import the js via express in node. Here is about connection and simple socket emit from server (app.js)

io.of('/'+path)
            .on('connection', function (socket) {
                socket.on('login', function(data) {
                        //process the data
                }
         }

From client side, just call it like

Var socket = io.connect(this.url);
socket.emit('login', "{"USER: "AD", "PASSWORD":"12"}");

A whole lot can be learnt from their documentation, which includes how to install, integration with node and others.

Hope you will give a try.

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • 1
    Yes, I'm trying this tut right now http://code.tutsplus.com/tutorials/using-nodejs-and-websockets-to-build-a-chat-service--net-34482 and it involves socket.io. So much potential in these technologies. It's cool to be on the leading generation of this stuff. Rare opportunities to do things never done before, the way I see it. – CuriousWebDeveloper Feb 28 '14 at 21:50
0

Websocket is the paradigm. You have the client emit events to the server and the server emit events to the client, essentially extending the client-side paradigm of JS events to go across a network.

Here's a good tutorial to try it out in:

http://code.tutsplus.com/tutorials/using-nodejs-and-websockets-to-build-a-chat-service--net-34482

Andrew Templeton
  • 1,656
  • 10
  • 13