Class Channel<Value>

Type Parameters

Constructors

Properties

Methods

Constructors

  • Create a new Channel

    Type Parameters

    Parameters

    • Optional id: null | number
    • Optional initial: Content

    Returns Channel<Value>

    Example

    import { Server, Channel } from "rjweb-server"
    import { Runtime } from "@rjweb/runtime-bun"

    const echo = new Channel<string>()

    const server = new Server(Runtime, {
    port: 8000
    })

    server.path('/', (path) => path
    .ws('/echo', (ws) => ws
    .onOpen((ctr) => {
    ctr.printChannel(echo)
    })
    .onMessage((ctr) => {
    echo.send(ctr.rawMessage()) // will send the message to all subscribed sockets
    })
    )
    .http('GET', '/last-echo', (http) => http
    .onRequest((ctr) => {
    return ctr.print(echo.last())
    })
    )
    )

    server.start().then(() => console.log('Server Started!'))

    Since

    9.0.0

Properties

data: null | Content
id: number

The ID of the Channel used for publishing

Since

9.0.0

listeners: Listener<Value>[] = []
onPublish: null | ((type, data) => void) = null

Type declaration

    • (type, data): void
    • Parameters

      • type: "binary" | "text"
      • data: ArrayBuffer

      Returns void

Methods

  • Listen for send events on this Channel

    Parameters

    • callback: Listener<Value>

    Returns (() => void)

      • (): void
      • Returns void

    Since

    9.0.0