As mentioned in Following payments using EventStream I am able to receive the payment stream on the server side, now what if I want to return only new trisections from the time I start the stream? Is that possible?

var EventSource = require('eventsource');
var es = new EventSource('https://horizon-testnet.stellar.org/accounts/GB7JFK56QXQ4DVJRNPDBXABNG3IVKIXWWJJRJICHRU22Z5R5PI65GAK3/payments');
es.onmessage = function(message) {
    var result = message.data ? JSON.parse(message.data) : message;
    console.log('New payment:');
    console.log(result);
};
es.onerror = function(error) {
    console.log('An error occured!');
}
up vote 2 down vote accepted

Yes. Add the parameter cursor=now to your request.

If you expect it may be in the future or the very recent past, you can run two streams (concurrently or in serial if you like), one with cursor=now&order=desc & consume events until you've found your expected payment or the timestamp is too early. And another with cursor=now&order=asc to check for future payments.

See the documented arguments.

  • But how can I make sure I am not missing the transaction as the confirmation time is seconds? My intention is to start/stop event stream on demand. – niksmac Oct 2 at 12:57
  • 1
    The api should give you access to the event id. When restarting, use the event id from the most recently seen event as the cursor. – Synesso Oct 2 at 21:24
  • My intention is not to keep it up and running along with the backend, but to start only when I am expecting a payment and stop as soon as I have the payment; possible? – niksmac Oct 3 at 4:48
  • 1
    Yes. If you expect it may be in the future or the very recent past, you can run two streams (concurrently or in serial if you like), one with cursor=now&order=desc & consume events until you've found your expected payment or the timestamp is too early. And another with cursor=now&order=asc to check for future payments. – Synesso Oct 3 at 7:58
  • 1
    Solved: I am getting old transaction even with cursor=now&order=asc using JS SDK it was my mistake. – niksmac Oct 6 at 5:43

As pointed out by the "Warning" on the page, you may consider switching to using the official JS SDK:

var lastCursor = "now"
var accountAddress = GB7JFK56QXQ4DVJRNPDBXABNG3IVKIXWWJJRJICHRU22Z5R5PI65GAK3

var es = server.transactions()
  .forAccount(accountAddress)
  .cursor(lastCursor)
  .stream({
      onmessage: txHandler
  })

lastCursor="now" gives you only new coming messages. You may try with using other values for lastCursor, other than zero (which will push out all the related info since the start of the chain).

  • But how can I make sure I am not missing the transaction as the confirmation time is seconds? My intention is to start/stop event stream on demand. – niksmac Oct 2 at 12:56
  • A cursor other than now uniquely identifies an event. Use the cursor value of the most recently seen event and you won't miss any. – Synesso Oct 2 at 21:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.