A WebSocket server and client for use in ilp-node, clp-frog, clp-cat, and similar software.
Inside your nodejs project, run:
npm install --save michielbdejong/clp-nodeThen in your nodejs script:
const ClpNode = require('ClpNode')Then there are basically three ways to use ClpNode:
const clpNode1 = new ClpNode({ listen: 8000 }, (ws, whoAmI, url) => {
console.log('I am the ' + whoAmI /* 'server' */ + ' and a client has connected to ' + url)
ws.on('message', (message) => {
ws.send('you said:')
ws.send(message)
})
})
``
## Listen on wss://my.domain.com with on-the-fly LetsEncrypt registration
Make sure you point the DNS domain to this server first, and ssh into your
server:
```sh
ssh root@my.domain.comThen on your server, save the following nodejs script:
const clpNode2 = new ClpNode({ tls: 'my.domain.com' }, (ws, whoAmI, url) => {
console.log('I am the ' + whoAmI /* 'server' */ + ' and a client has connected to ' + url)
console.log('a client has connected!')
ws.on('message', (message) => {
ws.send('you said:')
ws.send(message)
})
})const clpNode3 = new ClpNode({ upstreams: [ { url: 'wss://my.domain.com', path: '/' } ] }, (ws, whoAmI, url) => {
console.log('I am the ' + whoAmI /* 'client' */ + ' and I have connected to ' + url)
ws.on('message', (message) => {
ws.send('thanks')
})
ws.send('hello')
})You can also specify multiple upstreams, even if the node is already a server itself. In a future version we might add better support for that, so you can see which of your peers is connected when your connect-callback is called.
To start the clp node means that it starts listening as a server, and/or connects to the upstreams you configured. To stop the clp node means all connections, upstream and downstream, are closed again.
clpNode.start().then(() => {
console.log('clp node started', clpNode.config)
return new Promise((resolve) => { setTimeout(resolve, 10000) })
}).then(() => {
return clpNode.stop()
}).then(() => {
console.log('clp node stopped')
})