Braidify
Easily add the Braid Protocol to existing Javascript.
Purpose
Whereas Braid is "a few simple extensions to HTTP that
add synchronization"; the braidify
library is "a few simple extensions to
HTTP libraries that add Braid synchronization".
Braidify currently supports Braid in the following libraries:
require('braidify').fetch
require('braidify').http
We would love to support your favorite library, too.
Let's see how to use it:
Browser fetch()
<script src="braidify-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:
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()
You can also use for await
:
async function connect () {
try {
for await (var v of fetch('/chat', {subscribe: {keep_alive: true}})) {
if (v.patches)
chat = apply_patches(v.patches, chat)
else
chat = JSON.parse(v.body)
render_stuff()
}
} catch (e) {
console.log('Reconnecting...')
setTimeout(connect, 4000)
}
}
Nodejs client with fetch()
var fetch = require('braidify').fetch
import {fetch} from 'braidify'
fetch('https://localhost:3009/chat',
{subscribe: {keep_alive: true}}).andThen(
x => console.log('Got ', x)
)
Note: the current version of node-fetch
doesn't properly throw errors when a
response connection dies, and thus you cannot attach a .catch()
handler to
automatically reconnect. (See
issue #980 and
#753.) We recommend
using the http
library (below) for requests on nodejs instead.
Nodejs client with require('http')
var https = require('braidify').http(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()
Nodejs server using require('express')
On the server using express:
var braidify = require('braidify').http_server
import {http_server as braidify} from 'braidify'
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)
Nodejs server with require('http')
On the server using regular require('http')
:
var braidify = require('braidify').http_server
import {http_server as braidify} from 'braidify'
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)