Created
May 16, 2018 19:18
-
-
Save emschwartz/db7c8aee211ac9952ab41f48fbe587ce to your computer and use it in GitHub Desktop.
STREAM Example
This file contains hidden or 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 { 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)) |
This file contains hidden or 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 { 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