简体   繁体   中英

REST API and Real Time client

I want to have the following architecture:

A JSON REST API where real time statistic data is pushed to and stored in a Redis server.

A JSON REST API call where any number of clients (native or web) can receive this data after it has been stored - ie in real time.

The first client will just be a web app and I may build a native app later.

I'm wondering if my only option is for the clients to poll the REST API for changes? Ideally, I'd like the server to push updates as they arrive so I don't need to manage this polling.

Is my architecture suitable for what I want to achieve, or am I missing something?

A more efficient way than polling is to use websockets, such as Faye or Socket.IO . You can place an emit event under a data store event to immediately send data that's been stored.

With Socket.IO, you'd do that like this:

var io = require('socket.io').listen(80);
//note that you can listen on HTTP servers
//can also be used with Express applications, etc

//when data is stored, run this
io.sockets.emit('event', {
  object: 'that is sent to client'
});

You could then use this to tell the client that there is new data, or you could directly send the newly stored data. Custom events can be defined, such as

io.sockets.emit('data_receive', function (data) {...});

and would be received client side like so:

var socket = io.connect('http://socket.location');
socket.on('data_recieve, function (data) {
  //data is whatever sent from server
});

In Faye you'd do something like this:

var http = require('http');
var faye = require('faye');

var bayeux = new faye.NodeAdapter({
  mount: '/faye',
  timeout: 45
});
bayeux.listen(8000);

Then when data is stored, you'd run:

client.publish('/path', {
  data: 'Hello world'
});

Any client that has created a client like so:

var client = new Faye.Client('http://socket:port/path');

client.subscribe('/path', function(data) {
  alert('Received data: ' + data.text);
});

Will receive the data.

You have the option of Node.js and the websocket for push and pull in realtime. To don't manage the queuing you still have the option of MQ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM