Braidify
Using it
On the server using express:
var braidify = require('./protocols/http/http-server')
var app = require('express')()
app.use(braidify)
app.get('/', (req, res) => {
if (req.subscribe)
res.startSubscription({ onClose: _=> null })
else
res.statusCode = 200
res.sendVersion({
version: 'greg',
parents: ['gr','eg'],
body: JSON.stringify({greg: 'greg'})
})
})
require('http').createServer(app).listen(8583)
On the server using regular require('http')
:
var braidify = require('./protocols/http/http-server')
require('http').createServer(
(req, res) => {
braidify(req, res)
if (req.subscribe)
res.startSubscription({ onClose: _=> null })
else
res.statusCode = 200
res.sendVersion({
version: 'greg',
body: JSON.stringify({greg: 'greg'})
})
}
).listen(9935)
In a browser client:
<script src="http-client.js"></script>
<script>
fetch(
'https://braid.org/chat',
{subscribe: {keep_alive: true}},
).andThen(version => {
console.log('We got a new version!', version)
})
</script>
And if you want automatic reconnections, you can use:
function connect() {
fetch(
'https://braid.org/chat',
{subscribe: {keep_alive: true}},
).andThen(version => {
console.log('We got a new version!', version)
}).catch(e => setTimeout(connect, 1000))
}
connect()
On nodejs as a client:
var https = require('braidjs').braidify.http.client(require('https'))
https.get(
'https://braid.org/chat',
{subscribe: true},
(res) => {
res.on('version', (version) => {
console.log('well we got one', version)
})
}
)
To get auto-reconnections use:
function connect () {
https.get(
'https://braid.org/chat',
{subscribe: true},
(res) => {
res.on('version', (version) => {
console.log('We got a new version!', version)
})
res.on('end', e => setTimeout(connect, 1000))
res.on('error', e => setTimeout(connect, 1000))
})
}
connect()