1

In my application, I'm queueing client requests which the server will process one by one. But I only want the server to process requests that are 30 seconds old. So I created a unique path that clients push to and the server watches on child_added. Is there a way to get the timestamp when the child is added or any other metadata?

I know i can make the clients put the timestamp in the value but I don't want the clients to control the value because it can cause a security risk.

Update: clearly there is no firebase controlled timestamp or any other metadata attached to the node. The answer below is wrong.

duraid
  • 614
  • 7
  • 16

2 Answers2

6

You may use firebase server TIMESTAMP ,

firebaseRef.on('child_added', function(childSnapshot, prevChildName) {    
  // Update Firebase.ServerValue.TIMESTAMP on childSnapshot    
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Arup Saha
  • 179
  • 1
  • 3
  • This will mark the time when the message was received by the server. So if the server is offline and then wakes up to process messages it does not know how long they have been sitting in the queue. – duraid May 05 '15 at 16:56
  • Huh? The Firebase servers are real-time; they don't sleep. And it's unclear what you are expecting here. You don't want the clients to control the timestamp, but you also don't want the server to control the timestamp? Who is going to control it then? – Kato May 06 '15 at 15:24
  • @Kato Firebase. Firebase never sleeps but my servers do and when they wake up they only want to process messages that are new. – duraid May 08 '15 at 10:13
  • Check out [this question](http://stackoverflow.com/questions/19883736/how-to-discard-initial-data-in-a-firebase-db/27693310#27693310) for more on only processing new messages. But really, if you are using the queue model, they should be removed when processed. Thus, there should never be old items. – Kato May 08 '15 at 17:55
  • Thanks @Kato but in my case who would set the timestamp? the client? and how can I trust that? I even stated that in my question. Also, who would process the messages? the server? what if it's sleeping and there are messages in the queue? Another point: why are you marking this answer as correct when it is wrong? – duraid May 09 '15 at 11:46
  • 1
    Firebase.ServerValue.TIMESTAMP is set by Firebase when the data is entered. It is not set by your worker script or the client. Thus even if your server script is offline the timestamp will be set correctly. Also, this isn't an argument; everyone here is trying to help you; adjust your tone accordingly or you'll find volunteers few and far between. – Kato May 11 '15 at 18:09
0

Although not a direct answer to the question but the firebase queue which was introduced today can be used to achieve what was stated in the question. The _state_changed flag will serve as the timestamp.

https://www.firebase.com/blog/2015-05-15-introducing-firebase-queue.html

duraid
  • 614
  • 7
  • 16