2

I have a simple but unusual problem.

  1. I have a client page making requests to a RESTful API. So the client PUTs, no big deal. A form sends JSON to the API, and from there it is stored in the server database.

  2. However, I have a separate page that is supposed to give me a current status from the database, say, a count of rows. I need the count to be updated every time a new row is created. The current solution is to send a GET request every 5 seconds or so, which is obviously not ideal. I don't want to query the database if there is no row created yet.

So I need a trigger on every row create. This seems trivial to implement in app/api/Controllers/AppController.js. However, in this file I have a reference to the PUTting client, not the GETting one. How can I reference the GETting client there?

dmvianna
  • 15,088
  • 18
  • 77
  • 106
  • how about make your update request have a custom response that including row count? – lucas Jun 25 '15 at 00:24
  • 1
    possible duplicate of [How do I subscribe to a model instance in Sails.JS?](http://stackoverflow.com/questions/19818953/how-do-i-subscribe-to-a-model-instance-in-sails-js) – dmvianna Jun 25 '15 at 01:53
  • 1
    @dmvianna why wont you use native Sails [Websockets](http://sailsjs.org/documentation/reference/web-sockets/socket-client/io-socket-on)? – Bulkin Jun 29 '15 at 06:48
  • @Bulkin, because until last week these docs were just dead links. Thanks! :) – dmvianna Jun 29 '15 at 08:28

1 Answers1

2

It sounds like you're referring to a push service, which would need to be implemented server-side.

If the putting and status update page are supposed to be part of the same session you could store a cookie when storing data. Then the status page could poll the cookie so it knows when to make a new AJAX request. You'd still have client-side constant polling, but since the cookie keeps track of if you need to update or not it'd mean a lot less network calls.

But that would only work if this is supposed to be part of the same session. Otherwise you'd need either constant AJAX polling or some sort of push service.

Jan
  • 5,688
  • 3
  • 27
  • 44
  • Different sessions. How could I implement a push service? – dmvianna Jun 25 '15 at 01:24
  • 1
    Here's a little something to get you started. http://stackoverflow.com/questions/5195452/websockets-vs-server-sent-events-eventsource – Jan Jun 25 '15 at 14:25