0

I am building a mobile application that receives data from server whenever the server has a new data available. I am using cordova and ionic framework for my mobile application and a PHP/MySQL/Apache for the API and server which the mobile application fetches data.

Is there any way on how can I retrieve data (JSON format) from my server without constantly requesting data every nth seconds using http.get in my mobile application? Since I only need to fetch whenever it has a new data, and not all the time it has new data but sometimes, when on peak it has new data every second. Is Apache/PHP can handle this or do I need to switch to say for example nodejs or something? Thanks in advance.

BTW, i want my mobile application receives the data within a second.

My question is quite similar with this Receive update from server with mobile framework and https://softwareengineering.stackexchange.com/questions/225589/how-to-refresh-keep-up-to-date-content-in-the-browser-without-overloading-the-se but I'm still hanging right now.

Community
  • 1
  • 1
Port 8080
  • 858
  • 3
  • 12
  • 24
  • 2
    You need to use websockets for server-side push mechanism. You can have a look at [socket.io](http://socket.io/) – frank Oct 09 '14 at 15:32
  • Is this applicable with PHP and Apache? Or do I really need to switch to nodejs? – Port 8080 Oct 09 '14 at 15:35
  • You don't need websockets for server push, but it is the most efficient transport. For example, long-polling could also satisfy the requirement. – Timothy Strimple Oct 09 '14 at 15:36
  • 1
    http://stackoverflow.com/questions/333664/how-to-implement-basic-long-polling – Port 8080 Oct 09 '14 at 15:45
  • @frank Depending on if you needed the full duplex communication, you may be able to get away with [server-sent events](https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events) rather than websockets. This only allows one way communication though so if you're looking to have a full back-and-forth conversation with the server, you'll still need websockets. – War10ck Oct 10 '14 at 13:25

1 Answers1

1

Node.js and Socket.io makes something like this almost trivial, however you can do this with pretty much any web backend. There are a few options for doing this, but I lean towards websockets whenever I can. I have never used it, but Ratchet seems to do what you want for PHP. Take a look at their Hello World tutorial to see how to set it up.

update

Since you're using Cordova, websockets make sense. Here is a sample implementation in Node.js using socket.io.

var app = require('http').createServer(handler);
var io = require('socket.io')(app);

app.listen(80);

function handler (req, res) {
  // We aren't serving any http requests outside of socket.io. 
  // Return a 404 error for all other requests.
  res.status(404).send('Not found');
}

io.on('connection', function (socket) {
  //  This is either a new client connection, or a client reconnecting. 
  //  You can use cookies to establish identity and handle reconnects 
  //  differently if necessary.

  socket.on('new-content', function(content) {
    //  persist the file in the database if necessary and resend it to all 
    //  connected clients
    socket.broadcast.emit('new-content', content);
  });
});

Here we're just creating a simple relay server. You can send a 'file' which in this case is just a string of content, and it will get sent out to all other connected users. In this scenario you don't have to keep querying the database to look for new content, you can trigger it off of the content that is coming in from the clients. If you want offline users to be able to receive that content when they come online, you'll need some sort of persistence and mechanism for tracking and handling that.

The client side script is pretty simple as well.

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://myserver.com');
  socket.on('new-file function (content) {
    // A new file was sent from the server, do something with the content here.
  });

  function sendFile(content) {
    socket.emit('new-file', content);  
  }
</script>
Community
  • 1
  • 1
Timothy Strimple
  • 22,920
  • 6
  • 69
  • 76
  • can you throw some light on alternatives to websockets(socket.io), that could be used to solve the OPs requirement. – frank Oct 09 '14 at 15:44
  • Yes, is there any light alternatives to web sockets? – Port 8080 Oct 09 '14 at 15:49
  • First of all, socket.io isn't just websockets. It's an abstraction over multiple server push technologies. This enables the the developer to write "server push" code one way, and socket.io will upgrade to the most efficient method supported by both the server and the client. – Timothy Strimple Oct 09 '14 at 15:50
  • @Port8080 Websockets is actually the lightest implementation of server push technology. Unfortunately there seems to be a lot of boilerplate code necessary to get it working in PHP. The big advantage of using node / socket.io is it requires dramatically fewer lines of code to get websockets with failover working. Take a look at the docs and compare to Ratchet above. http://socket.io/docs/ – Timothy Strimple Oct 09 '14 at 15:54
  • Sorry for this noob question. Since I am developing a mobile application using Cordova does it mean the code for socket.io needs to reside in my mobile app or it should sits on my web server? This is just based on examples in here http://socket.io/docs/ since I am not familiar yet with socket.io – Port 8080 Oct 09 '14 at 15:56
  • @TimothyStrimple but if it is really hard to make it work with PHP then maybe it's time to switch to node. Since, my php application only outputs JSON data which in that case maybe node can handle if I am not mistaken – Port 8080 Oct 09 '14 at 15:58
  • How is that JSON data generated? What happens which causes the server to send out a new JSON packet to connected clients? – Timothy Strimple Oct 09 '14 at 16:03
  • @TimothyStrimple JSON data is generated when users of mobile application added something which then uploaded to the server. Then in my PHP application, it will query the db for new data and send it back (JSON) to all other users. – Port 8080 Oct 10 '14 at 05:52
  • Updated with sample socket.io code. Seems like a pretty straightforward use case and the server implementation should be pretty simple relative to a PHP websocket server. – Timothy Strimple Oct 10 '14 at 13:19