hypercast
v0.2.1
Published
Client looks like this.
Downloads
4
Readme
hypercast
Client looks like this.
// client.ts
import { Operator, Hypercast } from 'hypercast/client'
enum MessageType {
Hello = 'hello',
Goodbye = 'goodbye',
}
type Messages =
| {
type: MessageType.Hello
name: string
}
| {
type: MessageType.Goodbye
name: string
}
const operator = new Operator()
const { topic } = await operator.createTopic()
const hypercast = new Hypercast(topic)
hypercast.on('message', (event) => {
switch (event.data.type) {
case MessageType.Hello:
console.log(`Hello ${event.data.name}`)
case MessageType.GoodBye:
console.log(`Hello ${event.data.name}`)
default:
console.log(`Unknown message type`)
}
})
await hypercast.connect()
Then, to send a message:
const ack = hypercast.send({
type: MessageType.Hello,
name: 'Cloudflare',
})
// optional
await ack
Self Hosting a Hypercast Instance
Server looks like this, plus a little Wrangler config for the Durable Object.
// worker.ts
import { Operator } from 'hypercast/server'
export { Hypercast } from 'hypercast/server'
export default new Operator('/hypercast') // pathname, if any
And when creating your client, you need to pass in your URL:
const hypercast = new Hypercast(topic, 'https://esb.lol/hypercast')
Or:
const operator = new Operator('https://esb.lol/hypercast')
That's the whole thing.