hypercast
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i hypercast

Weekly Downloads

2

Version

0.2.1

License

MIT

Unpacked Size

76.3 kB

Total Files

14

Last publish

Collaborators

  • estrattonbailey