spoke/mqtt

Contains all the data types needed to work with spoke MQTT libraries. This makes it possible to write common code for different targets, and reduces code duplication.

Types

Authentication details passed to the server when connecting. Remember that these are not encrypted, unless working with an encrypted transport channel.

pub type AuthDetails {
  AuthDetails(
    username: String,
    password: option.Option(BitArray),
  )
}

Constructors

  • AuthDetails(username: String, password: option.Option(BitArray))

Error code from the server - we got a response, but there was an error.

pub type ConnectError {
  UnacceptableProtocolVersion
  IdentifierRefused
  ServerUnavailable
  BadUsernameOrPassword
  NotAuthorized
}

Constructors

  • UnacceptableProtocolVersion

    The MQTT server doesn’t support MQTT 3.1.1

  • IdentifierRefused

    The Client identifier is correct UTF-8 but not allowed by the Server

  • ServerUnavailable

    The Network Connection has been made but the MQTT service is unavailable

  • BadUsernameOrPassword

    The data in the user name or password is malformed

  • NotAuthorized

    The Client is not authorized to connect

The set of options used to establish a connection to the server. Includes the set of data that should generally not change across multiple connect calls (if they are needed).

pub type ConnectOptions(t) {
  ConnectOptions(
    transport_options: t,
    client_id: String,
    authentication: option.Option(AuthDetails),
    keep_alive_seconds: Int,
    server_timeout_ms: Int,
  )
}

Constructors

  • ConnectOptions(
      transport_options: t,
      client_id: String,
      authentication: option.Option(AuthDetails),
      keep_alive_seconds: Int,
      server_timeout_ms: Int,
    )

    Arguments

    transport_options

    Transport options, specific to runtime.

    client_id

    The MQTT client ID used when connecting to the server.

    authentication

    Optional username and (additionally optional) password

    keep_alive_seconds

    Keep-alive interval in seconds (MQTT spec doesn’t allow more granular control)

    server_timeout_ms

    “Reasonable amount of time” for the server to respond (including network latency), as used in the MQTT specification.

Represents the state of the connection to the server.

pub type ConnectionState {
  ConnectFailed(String)
  ConnectRejected(ConnectError)
  ConnectAccepted(SessionPresence)
  Disconnected
  DisconnectedUnexpectedly(reason: String)
}

Constructors

  • ConnectFailed(String)

    Connecting to the server failed before we got a response to the connect packet.

  • ConnectRejected(ConnectError)

    The server was reachable, but rejected our connect packet

  • ConnectAccepted(SessionPresence)

    The server has accepted our connect packet

  • Disconnected

    Disconnected as a result of calling disconnect

  • DisconnectedUnexpectedly(reason: String)

    The connection was dropped for an unexpected reason, e.g. a transport channel error or protocol violation.

Unified error type for operations initiated by the user of the library.

pub type OperationError {
  NotConnected
  OperationTimedOut
  ProtocolViolation
  SessionReset
  ClientRuntimeError(String)
}

Constructors

  • NotConnected

    The client was not connected when it was required.

  • OperationTimedOut

    The operation did not complete in time.

  • ProtocolViolation

    We received unexpected data from the server, and will disconnect.

  • SessionReset

    The session was reset, invalidating the operation

  • ClientRuntimeError(String)

    The client runtime ran into an error. These errors are most likely not recoverable.

A convenience record to hold all the data used when publishing messages.

pub type PublishData {
  PublishData(
    topic: String,
    payload: BitArray,
    qos: QoS,
    retain: Bool,
  )
}

Constructors

  • PublishData(
      topic: String,
      payload: BitArray,
      qos: QoS,
      retain: Bool,
    )

Quality of Service levels, as specified in the MQTT specification

pub type QoS {
  AtMostOnce
  AtLeastOnce
  ExactlyOnce
}

Constructors

  • AtMostOnce

    The message is delivered according to the capabilities of the underlying network. No response is sent by the receiver and no retry is performed by the sender. The message arrives at the receiver either once or not at all.

  • AtLeastOnce

    This quality of service ensures that the message arrives at the receiver at least once.

  • ExactlyOnce

    This is the highest quality of service, for use when neither loss nor duplication of messages are acceptable. There is an increased overhead associated with this quality of service.

Strongly typed boolean for session presence, for nicer type signatures.

pub type SessionPresence {
  SessionPresent
  SessionNotPresent
}

Constructors

  • SessionPresent
  • SessionNotPresent

Utility record for the data required to request a subscription.

pub type SubscribeRequest {
  SubscribeRequest(filter: String, qos: QoS)
}

Constructors

  • SubscribeRequest(filter: String, qos: QoS)

The result of a subscribe operation

pub type Subscription {
  SuccessfulSubscription(topic_filter: String, qos: QoS)
  FailedSubscription(topic_filter: String)
}

Constructors

  • SuccessfulSubscription(topic_filter: String, qos: QoS)

    The subscribe succeeded with the specified QoS level.

  • FailedSubscription(topic_filter: String)

    The server returned a failure for requested subscription.

Represents a received message or change in the client.

pub type Update {
  ReceivedMessage(
    topic: String,
    payload: BitArray,
    retained: Bool,
  )
  ConnectionStateChanged(ConnectionState)
}

Constructors

  • ReceivedMessage(topic: String, payload: BitArray, retained: Bool)

    A published message to a topic this client was subscribed to was received.

  • ConnectionStateChanged(ConnectionState)

    The connection state of this client changed.

Values

pub fn connect_with_id(
  transport_options: t,
  client_id: String,
) -> ConnectOptions(t)

Constructs connect options from transport options, the given client id, and default settings for the rest of the options.

pub fn keep_alive_seconds(
  options: ConnectOptions(t),
  keep_alive_seconds: Int,
) -> ConnectOptions(t)

Builder function for specifying the keep-alive time in the connect options.

pub fn server_timeout_ms(
  options: ConnectOptions(t),
  server_timeout_ms: Int,
) -> ConnectOptions(t)

Builder function for specifying the server operation timeout in the connect options.

pub fn using_auth(
  options: ConnectOptions(t),
  username: String,
  password: option.Option(BitArray),
) -> ConnectOptions(t)

Builder function for specifying the authentication details to be used when connecting.

Search Document