Presence

public final class Presence

The Presence object provides features for syncing presence information from the server with the client and handling presences joining and leaving.

Syncing state from the server

To sync presence state from the server, first instantiate an object and pass your channel in to track lifecycle events:

let channel = socket.channel("some:topic")
let presence = Presence(channel)

If you have custom syncing state events, you can configure the Presence object to use those instead.

let options = Options(events: [.state: "my_state", .diff: "my_diff"])
let presence = Presence(channel, opts: options)

Next, use the presence.onSync callback to react to state changes from the server. For example, to render the list of users every time the list changes, you could write:

presence.onSync { renderUsers(presence.list()) }

Listing Presences

presence.list is used to return a list of presence information based on the local state of metadata. By default, all presence metadata is returned, but a listBy function can be supplied to allow the client to select which metadata to use for a given presence. For example, you may have a user online from different devices with a metadata status of “online”, but they have set themselves to “away” on another device. In this case, the app may choose to use the “away” status for what appears on the UI. The example below defines a listBy function which prioritizes the first metadata which was registered for each user. This could be the first tab they opened, or the first device they came online from:

let listBy: (String, Presence.Map) -> Presence.Meta = { id, pres in
    let first = pres["metas"]!.first!
    first["count"] = pres["metas"]!.count
    first["id"] = id
    return first
}
let onlineUsers = presence.list(by: listBy)

(NOTE: The underlying behavior is a map on the presence.state. You are mapping the state dictionary into whatever datastructure suites your needs)

Handling individual presence join and leave events

The presence.onJoin and presence.onLeave callbacks can be used to react to individual presences joining and leaving the app. For example:

let presence = Presence(channel)
presence.onJoin { [weak self] (key, current, newPres) in
    if let cur = current {
        print("user additional presence", cur)
    } else {
        print("user entered for the first time", newPres)
    }
}

presence.onLeave { [weak self] (key, current, leftPres) in
    if current["metas"]?.isEmpty == true {
        print("user has left from all devices", leftPres)
    } else {
        print("user left from a device", current)
    }
}

presence.onSync { renderUsers(presence.list()) }

Enums and Structs

  • Custom options that can be provided when creating Presence

    Example:

    let options = Options(events: [.state: "my_state", .diff: "my_diff"])
    let presence = Presence(channel, opts: options)
    
    See more

    Declaration

    Swift

    public struct Options
  • Presense Events

    See more

    Declaration

    Swift

    public enum Events : String

Typaliases

  • Meta details of a Presence. Just a dictionary of properties

    Declaration

    Swift

    public typealias Meta = [String : Any]
  • Map

    A mapping of a String to an array of Metas. e.g. {“metas”: [{id: 1}]}

    Declaration

    Swift

    public typealias Map = [String : [Meta]]
  • A mapping of a Presence state to a mapping of Metas

    Declaration

    Swift

    public typealias State = [String : Map]
  • Undocumented

    Declaration

    Swift

    public typealias Diff = [String : State]
  • Closure signature of OnJoin callbacks

    Declaration

    Swift

    public typealias OnJoin = (_ key: String, _ current: Map?, _ new: Map) -> Void
  • Closure signature for OnLeave callbacks

    Declaration

    Swift

    public typealias OnLeave = (_ key: String, _ current: Map, _ left: Map) -> Void
  • / Closure signature for OnSync callbacks

    Declaration

    Swift

    public typealias OnSync = () -> Void

Properties

  • The state of the Presence

    Declaration

    Swift

    private(set) public var state: State
  • Pending join and leave diffs that need to be synced

    Declaration

    Swift

    private(set) public var pendingDiffs: [Diff]
  • The channel’s joinRef, set when state events occur

    Declaration

    Swift

    private(set) public var joinRef: String?
  • Undocumented

    Declaration

    Swift

    public var isPendingSyncState: Bool { get }
  • Callback to be informed of joins

    Declaration

    Swift

    public var onJoin: OnJoin { get set }
  • Set the OnJoin callback

    Declaration

    Swift

    public func onJoin(_ callback: @escaping OnJoin)
  • Callback to be informed of leaves

    Declaration

    Swift

    public var onLeave: OnLeave { get set }
  • Set the OnLeave callback

    Declaration

    Swift

    public func onLeave(_ callback: @escaping OnLeave)
  • Callback to be informed of synces

    Declaration

    Swift

    public var onSync: OnSync { get set }
  • Set the OnSync callback

    Declaration

    Swift

    public func onSync(_ callback: @escaping OnSync)
  • Undocumented

    Declaration

    Swift

    public init(channel: Channel, opts: Options = Options.defaults)
  • Returns the array of presences, with deault selected metadata.

    Declaration

    Swift

    public func list() -> [Map]
  • Returns the array of presences, with selected metadata

    Declaration

    Swift

    public func list<T>(by transformer: (String, Map) -> T) -> [T]
  • Filter the Presence state with a given function

    Declaration

    Swift

    public func filter(by filter: ((String, Map) -> Bool)?) -> State

Static