ApiStream

abstract class ApiStream(source)

A class which can be used to handle events coming in over a streaming connection.

If you declare a public, global ApiStream property, Kobweb will automatically detect it and register it at compile time.

// src/api/Example.kt
// By default, the name of this stream will be "example", taken from the file path + name.
val exampleStream = object : ApiStream {
...
}

For technical readers, note that streams are NOT websockets. Instead, if configured, a Kobweb server will open a single web socket, which can then handle one (or more!) Kobweb streams. The API for streams mimics web sockets so the experience will feel similar, but they're not 1:1 the same.

We use this approach instead of websockets directly for two reasons:

  1. this potentially reduces the number of connections a server needs to manage even if a site wants to register multiple separate streams.

  2. Kobweb supports a live reloading experience, and we cannot easily dynamically create and destroy websocket handlers in ktor. However, we can create a single streaming endpoint and multiplex the incoming messages to the appropriate stream handlers.

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
class ClientConnectedContext(val stream: Stream, val env: Environment, val data: Data, val logger: Logger) : ApiStream.StreamContext
Link copied to clipboard
class ClientDisconnectedContext(val stream: LimitedStream, val env: Environment, val data: Data, val logger: Logger) : ApiStream.LimitedStreamContext
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
class TextReceivedContext(val stream: Stream, val text: String, val env: Environment, val data: Data, val logger: Logger) : ApiStream.StreamContext

Functions

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard