1

I am new To Node js and I need a quick help. I have a node.js app receiving data from another app through a GET request like this: GET /api/users?data=26' - every 30 sesonds. My node app code looks like this:

 var express = require('express');
 var app = express();
 var port = process.env.PORT || 8080
 app.set('views', __dirname + '/views');
 app.set('view engine', 'jade');
 var router = express.Router();

 router.get('/api/users', function(req, res) {
 var query = req.param('data');
 //I need to pass data to the view without refreshing browser
 });

 app.use(router);

 app.listen(port)

I need to pass the data to the view(I have a template home.jade) without a user refreshing the browser.So since the request every 30 seconds a user will see the data changing every 30 seconds without refreshing the browser. How do I accomplish this? Thank you in advance.

BrTkCa
  • 4,703
  • 3
  • 24
  • 45
sparks
  • 471
  • 1
  • 6
  • 19

2 Answers2

2

you could use sockets, you don't need to be an expert to implement it just try it out

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

io.on('connection', function (socket) { // connection event handler
  socket.emit('client-listener', { data: 'hello world' }); // send info to the client, you can send an object with whatever info you want.
  socket.on('server-listener', function (data) { //listening from the client
    console.log(data.text); // should print 'from client'
  });
});
//Client Side:
<script src="/socket.io/socket.io.js"></script> //reference to your script

<script>
  var socket = io.connect('http://localhost');
  socket.on('client-listener', function (data) {
    console.log(data); // should print hello world
    socket.emit('server-listener', { text: 'from client' }); //send data to the server
  });
</script>

those are the basics "features" for using sockets, you can learn more at socket.io

I'm not comfortable by doing ajax request every x seconds it doesn't feel right to me, take a look to this SO question and decide yourself which is the best approach you would like to follow.

Community
  • 1
  • 1
pedrommuller
  • 15,741
  • 10
  • 76
  • 126
  • Thanks Jack that is what i need but in my case how would i listen for incoming request from the other app( `GET /api/users?data=26`)? – sparks Apr 20 '16 at 15:40
  • I think i will definetely use websockects .I am assuming `io.on('connection', function (socket)` listens for any request whether is coming from the browser or api call ,is that right? is 'connection' a keyword? – sparks Apr 20 '16 at 15:57
  • you can start by doing an emit inside the express request and then later you could move to a "better" and "cleaner" approach while you get use to use sockets and understand the concept, look here http://stackoverflow.com/questions/20909778/how-to-use-socket-io-in-express-routes – pedrommuller Apr 20 '16 at 15:57
0

use AJAX Polling.

throw a script tag in that template. If you are using jQuery you can do something like...

window.setInterval(function() {
  // call http server, get payload data and bind to interface
  // $.get(...) or other ajax method
}, 30 * 1000)
emran
  • 902
  • 6
  • 12