Skip to content

Instantly share code, notes, and snippets.

@daverickdunn
Last active August 22, 2021 22:34
Show Gist options
  • Save daverickdunn/081a870fa02b4acc4fb07094cc8a52d9 to your computer and use it in GitHub Desktop.
Save daverickdunn/081a870fa02b4acc4fb07094cc8a52d9 to your computer and use it in GitHub Desktop.
Stellar JS SDK listen to server streamed EventSource events
const StellarSdk = require('stellar-sdk');
StellarSdk.Network.useTestNetwork(); // StellarSdk.Network.usePublicNetwork();
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); // const server = new StellarSdk.Server('https://horizon.stellar.org');
// convenience method: returns a PaymentCallBuilder
// https://stellar.github.io/js-stellar-sdk/PaymentCallBuilder_PaymentCallBuilder.html#stream
// there are similar CallBuilders for other network events, this one is specific to payments
var stream = server
.payments()
.cursor('now')
.stream({
onmessage: message => {
console.log(message)
},
onerror: err => {
console.log(err)
}
});
// if listening for a particular event, you may want to wrap the above in a promise until the event has either occured or timed out.
// in that case, to properly close the stream, you simply call the returned function 'stream', i.e. stream()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment