lily/transport/http

HTTP/SSE transport for client-server communication. Uses Server-Sent Events (SSE) for server to client and POST for client to server. Useful when WebSocket connections are blocked by corporate firewalls.

The transport uses Server-Sent Events (SSE) via the EventSource API for server to client messages and POST requests with JSON payloads for client to server messages. Connection status is tracked via SSE onopen and onerror events, and messages persist in localStorage while disconnected.

import lily/client
import lily/transport
import lily/transport/http

pub fn main() {
  let runtime = client.start(app_store)

  runtime
  |> client.connect(
    with: http.config(
      post_url: "http://localhost:8080/api/messages",
      events_url: "http://localhost:8080/events",
    )
    |> http.connect,
    serialiser: transport.automatic(),
  )
}

This is a client-side transport only. You still need a server library like mist to handle the SSE endpoint and POST requests.

HTTP transport is JavaScript-only (@target(javascript)).

Types

Configuration for HTTP transport. Requires both a POST URL (for client to server messages) and an SSE events URL (for server to client messages).

pub opaque type Config

Values

pub fn config(
  post_url post_url: String,
  events_url events_url: String,
) -> Config

Create a new HTTP transport configuration. The post_url is used for sending messages to the server (client to server), and the events_url is used for receiving Server-Sent Events (server to client).

Example

http.config(
  post_url: "/api/messages",
  events_url: "/api/events",
)
pub fn connect(
  config: Config,
) -> fn(transport.Handler) -> transport.Transport

Returns a connector function that establishes an HTTP/SSE connection. This connector can be passed to client.connect.

Example

client.connect(
  with: http.config(
    post_url: "/api/messages",
    events_url: "/api/events",
  )
    |> http.connect,
  serialiser: my_serialiser,
)
Search Document