Published
Edited
Feb 9, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
fetch(`https://patchbay.pub/pubsub/${id1}`, {
body: `Hello world ${++mutable publisher1_message_count}`,
headers: {"content-type": "text/plain"},
method: "POST"
}).then(response => {
return Object.fromEntries(response.headers.entries())
})
Insert cell
Insert cell
fetch(`https://patchbay.pub/pubsub/${id1}`).then(response => response.text())
Insert cell
Insert cell
Insert cell
{
const url = `https://patchbay.pub/pubsub/${id2}`
const msg = body => ({headers: {"content-type": "text/plain"}, method: "POST", body})
// The server doesn't block a subscriber-less publisher
await fetch(url, msg("no subscribers will receive this message"))
// Launch the subscriber in the background since won't return response until there is a subscriber
const subscriber = fetch(url).then(response => response.text())
yield "Subscriber launched. Wait 3 seconds until publishing"
await Promises.delay(3000)
yield "Publishing..."
await fetch(`https://patchbay.pub/pubsub/${id2}`, msg("message #2"))
yield "Published"
yield subscriber
}
Insert cell
Insert cell
Insert cell
{
const url = `https://patchbay.pub/pubsub/${id3}`
const msg = body => ({headers: {"content-type": "text/plain"}, method: "POST", body})
const subscriber1 = fetch(url).then(response => response.text())
const subscriber2 = fetch(url).then(response => response.text())
yield "2 subscribers launched. Wait 3 seconds until publishing"
await Promises.delay(3000)
yield "Publishing..."
await fetch(url, msg("hello all subscribers"))
yield "Published"
yield Promise.all([subscriber1, subscriber2])
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more