Braid-HTTP
This polyfill library implements the Braid-HTTP v04 protocol in Javascript. It gives browsers a braid_fetch()
drop-in replacement for the fetch()
API, and gives nodejs an http
plugin, allowing them to speak Braid in a simple way.
Developed in braid.org.
Installing
Browsers:
<script src="https://unpkg.com/braid-http/braid-http-client.js"></script>
<script>
</script>
Node.js:
npm install braid-http
require('braid-http').fetch
require('braid-http').http_client
require('braid-http').http_server
import {fetch, http_client, http_server} from 'braid-http'
Using it in Browsers
This library adds a {subscribe: true}
option to fetch()
, and lets you
access the result of a subscription with two new fields on the fetch response:
response.subscribe( update => ... )
response.subscription
: an iterator that can be used with for await
Example Subscription with Promises
Here is an example of subscribing to a Braid resource using promises:
fetch('https://braid.org/chat', {subscribe: true}).then(
res => res.subscribe(
(update) => {
console.log('We got a new update!', update)
}
)
)
If you want automatic reconnections, add two error handlers like this:
function connect() {
fetch('https://braid.org/chat', {subscribe: true}).then(
res => res.subscribe(
(update) => {
console.log('We got a new update!', update)
},
e => setTimeout(connect, 1000)
)
).catch(e => setTimeout(connect, 1000))
}
connect()
Example Subscription with Async/Await
async function connect () {
try {
(await fetch('/chat', {subscribe: true})).subscribe(
(update) => {
},
() => setTimeout(connect, 1000)
)
} catch (e) {
setTimeout(connect, 1000)
}
}
Example Subscription with for await
async function connect () {
try {
var subscription_iterator = fetch('/chat', {subscribe: true}).subscription
for await (var v of subscription_iterator) {
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)
}
}
Using it in Nodejs
Example Nodejs server with require('http')
Braidify adds these fields and methods to requests and responses:
req.subscribe
req.startSubscription({onClose: cb})
await req.parseUpdate()
res.sendUpdate()
Use it like this:
var braidify = require('braid-http').http_server
import {http_server as braidify} from 'braid-http'
require('http').createServer(
(req, res) => {
braidify(req, res)
if (req.subscribe)
res.startSubscription({ onClose: _=> null })
else
res.statusCode = 200
res.sendUpdate({
version: ['greg'],
body: JSON.stringify({greg: 'greg'})
})
}
).listen(9935)
Example Nodejs server with require('express')
With express
, you can simply call app.use(braidify)
to get braid features
added to every request and response.
var braidify = require('braid-http').http_server
import {http_server as braidify} from 'braid-http'
var app = require('express')()
app.use(braidify)
app.get('/', (req, res) => {
if (req.subscribe)
res.startSubscription({ onClose: _=> null })
else
res.statusCode = 200
res.sendUpdate({
version: ['greg'],
parents: ['gr','eg'],
body: JSON.stringify({greg: 'greg'})
})
})
require('http').createServer(app).listen(8583)
Example Nodejs client with require('http')
var https = require('braid-http').http_client(require('https'))
https.get(
'https://braid.org/chat',
{subscribe: true},
(res) => {
res.on('update', (update) => {
console.log('well we got one', update)
})
}
)
To get auto-reconnections use:
function connect () {
https.get(
'https://braid.org/chat',
{subscribe: true},
(res) => {
res.on('update', (update) => {
console.log('We got a new update!', update)
})
res.on('end', e => setTimeout(connect, 1000))
res.on('error', e => setTimeout(connect, 1000))
})
}
connect()
Example Nodejs client with fetch()
var fetch = require('braid-http').fetch
import {fetch} from 'braid-http'
fetch('https://localhost:3009/chat',
{subscribe: 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.