mist

Types

pub opaque type Builder(request_body, response_body)

The values returning from streaming the request body. The Chunk variant gives back some data and the next token. Done signifies that we have completed reading the body.

pub type Chunk {
  Chunk(
    data: BitString,
    consume: fn(Int) -> Result(Chunk, ReadError),
  )
  Done
}

Constructors

  • Chunk(
      data: BitString,
      consume: fn(Int) -> Result(Chunk, ReadError),
    )
  • Done

Re-exported type that represents the default Request body type. See mist.read_body to convert this type into a BitString. The Connection also holds some additional information about the request. Currently, the only useful field is client_ip which is a Result with a tuple of integers representing the IPv4 address.

pub type Connection =
  InternalConnection

Potential errors when opening a file to send. This list is currently not exhaustive with POSIX errors.

pub type FileError {
  IsDir
  NoAccess
  NoEntry
  UnknownFileError
}

Constructors

  • IsDir
  • NoAccess
  • NoEntry
  • UnknownFileError

The possible errors from reading the request body. If the size is larger than the provided value, ExcessBody is returned. If there is an error reading the body from the socket or the body is malformed (i.e a chunked request with invalid sizes), MalformedBody is returned.

pub type ReadError {
  ExcessBody
  MalformedBody
}

Constructors

  • ExcessBody
  • MalformedBody

The response body type. This allows mist to handle these different cases for you. Bytes is the regular data return. Websocket will upgrade the socket to websockets, but should not be used directly. See the mist.upgrade function for usage. Chunked will use Transfer-Encoding: chunked to send an iterator in chunks. File will use Erlang’s sendfile to more efficiently return a file to the client.

pub type ResponseData {
  Websocket(Selector(ProcessDown))
  Bytes(BitBuilder)
  Chunked(Iterator(BitBuilder))
  File(descriptor: file.FileDescriptor, offset: Int, length: Int)
}

Constructors

  • Websocket(Selector(ProcessDown))
  • Bytes(BitBuilder)
  • Chunked(Iterator(BitBuilder))
  • File(descriptor: file.FileDescriptor, offset: Int, length: Int)

    See mist.send_file to use this response type.

These are the types of messages that a websocket handler may receive.

pub type WebsocketMessage(custom) {
  Text(BitString)
  Binary(BitString)
  Closed
  Shutdown
  Custom(custom)
}

Constructors

  • Text(BitString)
  • Binary(BitString)
  • Closed
  • Shutdown
  • Custom(custom)

Functions

pub fn after_start(builder: Builder(a, b), after_start: fn(Int) ->
    Nil) -> Builder(a, b)

Override the default function to be called after the service starts. The default is to log a message with the listening port.

pub fn new(handler: fn(Request(a)) -> Response(b)) -> Builder(
  a,
  b,
)

Create a new mist handler with a given function. The default port is 4000.

pub fn port(builder: Builder(a, b), port: Int) -> Builder(a, b)

Assign a different listening port to the service.

pub fn read_body(req: Request(Connection), max_body_limit max_body_limit: Int) -> Result(
  Request(BitString),
  ReadError,
)

The request body is not pulled from the socket until requested. The content-length header is used to determine whether the socket is read from or not. The read may also fail, and a ReadError is raised.

pub fn read_request_body(builder: Builder(BitString, a), bytes_limit bytes_limit: Int, failure_response failure_response: Response(
    a,
  )) -> Builder(Connection, a)

This function allows for implicitly reading the body of requests up to a given size. If the size is too large, or the read fails, the provided failure_response will be sent back as the response.

pub fn send_binary_frame(connection: WebsocketConnection, frame: BitString) -> Result(
  Nil,
  SocketReason,
)

Sends a binary frame across the websocket.

pub fn send_file(path: String, offset offset: Int, limit limit: Option(
    Int,
  )) -> Result(ResponseData, FileError)

To respond with a file using Erlang’s sendfile, use this function with the specified offset and limit (optional). It will attempt to open the file for reading, get its file size, and then send the file. If the read errors, this will return the relevant FileError. Generally, this will be more memory efficient than manually doing this process with mist.Bytes.

pub fn send_text_frame(connection: WebsocketConnection, frame: BitString) -> Result(
  Nil,
  SocketReason,
)

Sends a text frame across the websocket.

pub fn start_http(builder: Builder(Connection, ResponseData)) -> Result(
  Nil,
  StartError,
)

Start a mist service over HTTP with the provided builder.

pub fn start_https(builder: Builder(Connection, ResponseData), certfile certfile: String, keyfile keyfile: String) -> Result(
  Nil,
  StartError,
)

Start a mist service over HTTPS with the provided builder. This method requires both a certificate file and a key file. The library will attempt to read these files off of the disk.

pub fn stream(req: Request(Connection)) -> Result(
  fn(Int) -> Result(Chunk, ReadError),
  ReadError,
)

Rather than explicitly reading either the whole body (optionally up to N bytes), this function allows you to consume a stream of the request body. Any errors reading the body will propagate out, or Chunks will be emitted. This provides a consume method to attempt to grab the next size chunk from the socket.

pub fn websocket(request request: Request(Connection), handler handler: fn(
    a,
    WebsocketConnection,
    WebsocketMessage(b),
  ) -> Next(b, a), on_init on_init: fn() ->
    #(a, Option(Selector(b))), on_close on_close: fn() -> Nil) -> Response(
  ResponseData,
)

Upgrade a request to handle websockets. If the request is malformed, or the websocket process fails to initialize, an empty 400 response will be sent to the client.

The on_init method will be called when the actual WebSocket process is started, and the return value is the initial state and an optional selector for receiving user messages.

The on_close method is called when the WebSocket process shuts down for any reason, valid or otherwise.

Search Document