Skip to content

Instantly share code, notes, and snippets.

@emschwartz
Created May 16, 2018 19:18
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save emschwartz/db7c8aee211ac9952ab41f48fbe587ce to your computer and use it in GitHub Desktop.
STREAM Example
const { createConnection } = require('ilp-protocol-stream')
const getPlugin = require('ilp-plugin')
async function run () {
const clientPlugin = getPlugin()
const connection = await createConnection({
plugin: clientPlugin,
// These are the ILP address and secret from the server
destinationAccount,
sharedSecret
})
const stream = connection.createStream()
stream.write('hello\n')
stream.write('here is some more data')
await stream.sendTotal(100)
await stream.sendTotal(200)
stream.end()
}
run().catch((err) => console.log(err))
const { createServer } = require('ilp-protocol-stream')
const getPlugin = require('ilp-plugin')
async function run () {
const serverPlugin = getPlugin()
const server = await createServer({
plugin: serverPlugin
})
// These need to be passed to the client through an encrypted communication channel
const { destinationAccount, sharedSecret } = server.generateAddressAndSecret()
server.on('connection', (connection) => {
console.log('got new connection')
connection.on('stream', (stream) => {
console.log(`got new stream: ${stream.id}`)
stream.setReceiveMax(10000)
stream.on('money', (amount) => {
console.log(`got money: ${amount}`)
})
stream.on('data', (chunk) => {
console.log('got data:', chunk.toString('utf8'))
})
stream.on('end', () => {
console.log('stream closed')
})
})
})
}
run().catch((err) => console.log(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment