EventBus

public class EventBus

Provides a connection to a remote TCP EventBus Bridge.

  • Creates a new EventBus instance.

    Note: a new EventBus instance isn’t connected to the bridge automatically. See connect().

    Declaration

    Swift

    public init(host: String, port: Int, pingEvery: Int = 5000)

    Parameters

    host

    address of host running the bridge

    port

    port the bridge is listening on

    pingEvery

    interval (in ms) to ping the bridge to ensure it is still up (default: 5000)

    Return Value

    a new EventBus

  • Connects to the remote bridge.

    Any already registered message handlers will be (re)connected.

    Throws

    Socket.Error if connecting fails

    Declaration

    Swift

    public func connect() throws
  • Disconnects from the remote bridge.

    Declaration

    Swift

    public func disconnect()
  • Signals the current state of the connection.

    Declaration

    Swift

    public func connected() -> Bool

    Return Value

    true if connected to the remote bridge, false otherwise

  • Sends a message to an EventBus address.

    If the remote handler will reply, you can provide a callback to handle that reply. If no reply is received within the specified timeout, a Response that responds with false to timeout() will be passed.

    Throws

    Error.invalidData(data:) if the given body can’t be converted to JSON

    Throws

    Error.disconnected(cause:) if not connected to the remote bridge

    Declaration

    Swift

    public func send(to address: String,
                         body: [String: Any],
                         headers: [String: String]? = nil,
                         replyTimeout: Int = 30000, // 30 seconds
                         callback: ((Response) -> ())? = nil) throws

    Parameters

    to

    the address to send the message to

    body

    the body of the message

    headers

    headers to send with the message (default: [String: String]())

    replyTimeout

    the timeout (in ms) to wait for a reply if a reply callback is provided (default: 30000)

    callback

    the callback to handle the reply or timeout Response (default: nil)

  • Publishes a message to the EventBus.

    Throws

    Error.invalidData(data:) if the given body can’t be converted to JSON

    Throws

    Error.disconnected(cause:) if not connected to the remote bridge

    Declaration

    Swift

    public func publish(to address: String,
                            body: [String: Any],
                            headers: [String: String]? = nil) throws

    Parameters

    to

    the address to send the message to

    body

    the body of the message

    headers

    headers to send with the message (default: [String: String]())

  • Registers a closure to receive messages for the given address.

    Throws

    Error.disconnected(cause:) if not connected to the remote bridge

    Declaration

    Swift

    public func register(address: String,
                             id: String? = nil,
                             headers: [String: String]? = nil,
                             handler: @escaping (Message) -> ()) throws -> String

    Parameters

    address

    the address to listen to

    id

    the id for the registration (default: a random uuid)

    headers

    headers to send with the register request (default: [String: String]())

    handler

    the closure to handle each Message

    Return Value

    an id for the registration that can be used to unregister it

  • Registers a closure to receive messages for the given address.

    Throws

    Error.disconnected(cause:) if not connected to the remote bridge

    Declaration

    Swift

    public func unregister(address: String, id: String, headers: [String: String]? = nil) throws -> Bool

    Parameters

    address

    the address to remove the registration from

    id

    the id for the registration

    headers

    headers to send with the unregister request (default: [String: String]())

    Return Value

    true if something was actually unregistered

  • Registers an error handler that will be passed any errors that occur in async operations.

    Operations that can trigger this error handler are:

    • handling messages received from the remote bridge (can trigger Error.disconnected(cause:) or Error.serverError(message:))
    • the ping operation discovering the bridge connection has closed (can trigger Error.disconnected(cause:))

    Declaration

    Swift

    public func register(errorHandler: @escaping (Error) -> ())

    Parameters

    errorHandler

    a closure that will be passed an Error when an error occurs

  • Represents error types thrown or passed by EventBus methods.

    See more

    Declaration

    Swift

    public enum Error : Swift.Error