Last active
August 22, 2021 22:34
-
-
Save daverickdunn/081a870fa02b4acc4fb07094cc8a52d9 to your computer and use it in GitHub Desktop.
Stellar JS SDK listen to server streamed EventSource events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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