Pushing data from server to client

144 views
Skip to first unread message

Niral Kalavadia

unread,
Nov 19, 2014, 7:00:32 AM11/19/14
to nod...@googlegroups.com
Hello everyone,

I am nee to node.js. Basically my background is Electronics. I am developing wireless sensor network in which there are 50 nodes (client) and one server. The main functionality that I want in project is each client upload some data to server when SERVER ASK THEM TO UPLOAD. This requirement leads me to explore node.js as it supports pushing data from server to client. While exploring node.js basic question arise in my mind. I'm looking forward to get answer from here.

How a server can send data to multiple client at a time..??

How should I begin to develop my server..??

At client side I am running my code on python (UDP client)

How should I proceed to solve this problem..?? At client side I can work with any protocol as I'm using Raspberry Pi.

Thank you,
Niral

Christopher Rust

unread,
Nov 19, 2014, 1:09:01 PM11/19/14
to nod...@googlegroups.com

Hello Niral,

I'd ask if it's necessary for the server to request the data from the clients. If not, then we can really use Node to it's strengths.

Using Node it's very easy to setup a REST JSON API. Search the internet for tutorials using the Express library.

Then on the client you can use Python to make HTTP requests to the Node server delivering the data in a JSON format. The beauty of using node for a situation like this is two fold: asynchronous and native JSON.

JSON is a common HTTP communication format. Drawing the line in the sand here, between your server and client, is commonly referred to as loose coupling. It doesn't matter what choice of language or tech you use on the client side as long as it communicates using JSON HTTP requests.

Asynchronous means that your Node server, if written correctly, can accept many requests at once. This will allow you to scale your sensor network to use a large number of clients.

If you truly need the server to make requests to the sensors then you'll need to setup a "server", to accept requests, on the client as well. Two-way communication will be more complex.

Chris

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/803edb18-ff77-44ce-9c4e-00e1e37766c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niral Kalavadia

unread,
Nov 20, 2014, 5:02:57 AM11/20/14
to nod...@googlegroups.com
Hello Chris,

Thank you very much for your inputs.

Yes it is necessary for server to ask for data to client. This is the only mandatory need of my network. Is it possible to use node.js in this application?? Is node capable of sending request to multiple client at a time???

FELIPE TORRES

unread,
Nov 20, 2014, 5:53:19 AM11/20/14
to nod...@googlegroups.com
To develop  a reliable two-side communication system, i'd look into socket.io as al alternative
I've used plenty of times and found little to no problems on implementing and develop with it.
Although you need a stable connection between the server and the client. 

I'd give it a go :)

Ryan Schmidt

unread,
Nov 20, 2014, 7:46:31 AM11/20/14
to nod...@googlegroups.com

On Nov 20, 2014, at 4:02 AM, Niral Kalavadia wrote:

> Yes it is necessary for server to ask for data to client. This is the only mandatory need of my network. Is it possible to use node.js in this application?? Is node capable of sending request to multiple client at a time???

Yes, and yes. You can write any code you want to, in most any language. :)

If your server is an http server written in node, and your client is a modern web browser, then the already-mentioned socket.io library is a popular way to achieve this goal. If your client and server are not talking http to one another, if your client is not a modern web browser, then you may need a different library, or you may need to write the communication code yourself.

Will Hoover

unread,
Nov 20, 2014, 10:11:48 AM11/20/14
to nod...@googlegroups.com
Just to add to Ryan's comments...

If you do decide to use a modern web browser as your client (and you don't care about Internet Exploder support) you can use SSEs. The beauty of using SSE is that if your server goes down or a connection is lost the SSE client will reconnect automatically:

require('http').createServer(function (req, res) {
   
if (req.headers.accept && req.headers.accept == 'text/event-stream') {
        res
.writeHead(200, {
           
'Content-Type': 'text/event-stream',
           
'Cache-Control': 'no-cache',
           
'Connection': 'keep-alive'
       
});
       
var id = 0;
        setInterval
(sse, 5000);
       
// or: process.nextTick(sse);
        sse
();
       
function sse() {
            res
.write('id: ' + ++id + '\n');
            res
.write("data: " + new Date().toUTCString() + '\n\n');
       
}
   
} else {
        res
.writeHead(200, { 'Content-Type': 'text/html' });
        res
.write('<!DOCTYPE html><html><head>' +
           
'<script>' +
               
'new EventSource("/").addEventListener("message",function(event) {' +
                   
'document.getElementById("sse").innerHTML=event.data' +
               
'});' +
           
'</script>' +
           
'</head><body><div id="sse">Waiting...</div></body></html>');
        res
.end();
   
}
}).listen(9080);

Will Hoover

unread,
Nov 20, 2014, 11:42:46 AM11/20/14
to nod...@googlegroups.com
Sorry, noticed that you want them sent at the same time:

var clients = [];

require('http').createServer(function (req, res) {
   
if (req.headers.accept && req.headers.accept == 'text/event-stream') {
        res
.writeHead(200, {
           
'Content-Type': 'text/event-stream',
           
'Cache-Control': 'no-cache',
           
'Connection': 'keep-alive'
       
});

        clients
.push(res);
        req
.on('close', function reqClosed() {
            clients
.splice(clients.indexOf(res), 1);

       
});
   
} else {
        res
.writeHead(200, { 'Content-Type': 'text/html' });
        res
.write('<!DOCTYPE html><html><head>' +
           
'<script>' +
               
'new EventSource("/").addEventListener("message",function(event) {' +
                   
'document.getElementById("sse").innerHTML=event.data' +
               
'});' +
           
'</script>' +
           
'</head><body><div id="sse">Waiting...</div></body></html>');
        res
.end();
   
}
}).listen(9080);

setInterval
(function sse() {
   
if (!clients.length) {
       
return;
   
}
   
var dt = new Date().toUTCString();
   
for (var i = 0; i < clients.length; i++) {
        clients
[i].write("data: " + dt + '\n\n');
   
}
}, 5000);

svante karlsson

unread,
Nov 20, 2014, 2:41:14 PM11/20/14
to nod...@googlegroups.com
Since you are speaking of sensor networks, MQTT is a "perfect" fit for what you want.

This can easily be done in any language (a lot at least) but if you would like js take a look at:

https://github.com/adamvr/MQTT.js/

The example is almost what you ask for. Let the client (sensor)  subscribe to a message - "sample"

Have another client (controller) running on the serverside that publishes those messages

client.publish('sample', 'ts');

all sensors will get that message more or less at the same time and they will then publish their samples under a topic like "deviceid/data"

works with both tcp and ssl, if you want to do this over public network and has a low protocol overhead.

/svante



--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.

Niral Kalavadia

unread,
Nov 20, 2014, 11:53:06 PM11/20/14
to nod...@googlegroups.com, sa...@csi.se
Thank you very much.. MQTT.js seems that it will do for me..

Niral Kalavadia

unread,
Nov 20, 2014, 11:53:41 PM11/20/14
to nod...@googlegroups.com
Hello Will,

Thank you very much..

Niral Kalavadia

unread,
Nov 20, 2014, 11:54:47 PM11/20/14
to nod...@googlegroups.com
Hello Felipe,

Thanks a lot for your inputs..
Reply all
Reply to author
Forward
0 new messages