Channel

public class Channel

Undocumented

  • The topic of the Channel. e.g. “rooms:friends”

    Declaration

    Swift

    public let topic: String
  • The params sent when joining the channel

    Declaration

    Swift

    public var params: Payload { get set }
  • Overridable message hook. Receives all events for specialized message handling before dispatching to the channel callbacks.

    • return: Must return the message, modified or unmodified

    Declaration

    Swift

    public var onMessage: (Message) -> Message

    Parameters

    msg

    The Message received by the client from the server

  • Joins the channel

    • return: Push event

    Declaration

    Swift

    @discardableResult
    public func join(timeout: TimeInterval? = nil) -> Push

    Parameters

    timeout

    Optional. Defaults to Channel’s timeout

  • Hook into when the Channel is closed. Does not handle retain cycles. Use delegateOnClose(to:) for automatic handling of retain cycles.

    Example:

    let channel = socket.channel("topic")
    channel.onClose() { [weak self] message in
        self?.print("Channel \(message.topic) has closed"
    }
    
    • return: Ref counter of the subscription. See func off()

    Declaration

    Swift

    @discardableResult
    public func onClose(_ callback: @escaping ((Message) -> Void)) -> Int

    Parameters

    callback

    Called when the Channel closes

  • Hook into when the Channel is closed. Automatically handles retain cycles. Use onClose() to handle yourself.

    Example:

    let channel = socket.channel("topic")
    channel.delegateOnClose(to: self) { (self, message) in
        self.print("Channel \(message.topic) has closed"
    }
    
    • return: Ref counter of the subscription. See func off()

    Declaration

    Swift

    @discardableResult
    public func delegateOnClose<Target: AnyObject>(to owner: Target,
                                                   callback: @escaping ((Target, Message) -> Void)) -> Int

    Parameters

    owner

    Class registering the callback. Usually self

    callback

    Called when the Channel closes

  • Hook into when the Channel receives an Error. Does not handle retain cycles. Use delegateOnError(to:) for automatic handling of retain cycles.

    Example:

    let channel = socket.channel("topic")
    channel.onError() { [weak self] (message) in
        self?.print("Channel \(message.topic) has errored"
    }
    
    • return: Ref counter of the subscription. See func off()

    Declaration

    Swift

    @discardableResult
    public func onError(_ callback: @escaping ((_ message: Message) -> Void)) -> Int

    Parameters

    callback

    Called when the Channel closes

  • Hook into when the Channel receives an Error. Automatically handles retain cycles. Use onError() to handle yourself.

    Example:

    let channel = socket.channel("topic")
    channel.delegateOnError(to: self) { (self, message) in
        self.print("Channel \(message.topic) has closed"
    }
    
    • return: Ref counter of the subscription. See func off()

    Declaration

    Swift

    @discardableResult
    public func delegateOnError<Target: AnyObject>(to owner: Target,
                                                   callback: @escaping ((Target, Message) -> Void)) -> Int

    Parameters

    owner

    Class registering the callback. Usually self

    callback

    Called when the Channel closes

  • Subscribes on channel events. Does not handle retain cycles. Use delegateOn(_:, to:) for automatic handling of retain cycles.

    Subscription returns a ref counter, which can be used later to unsubscribe the exact event listener

    Example:

    let channel = socket.channel("topic")
    let ref1 = channel.on("event") { [weak self] (message) in
        self?.print("do stuff")
    }
    let ref2 = channel.on("event") { [weak self] (message) in
        self?.print("do other stuff")
    }
    channel.off("event", ref1)
    

    Since unsubscription of ref1, “do stuff” won’t print, but “do other stuff” will keep on printing on the “event”

    • return: Ref counter of the subscription. See func off()

    Declaration

    Swift

    @discardableResult
    public func on(_ event: String, callback: @escaping ((Message) -> Void)) -> Int

    Parameters

    event

    Event to receive

    callback

    Called with the event’s message

  • Subscribes on channel events. Automatically handles retain cycles. Use on() to handle yourself.

    Subscription returns a ref counter, which can be used later to unsubscribe the exact event listener

    Example:

    let channel = socket.channel("topic")
    let ref1 = channel.delegateOn("event", to: self) { (self, message) in
        self?.print("do stuff")
    }
    let ref2 = channel.delegateOn("event", to: self) { (self, message) in
        self?.print("do other stuff")
    }
    channel.off("event", ref1)
    

    Since unsubscription of ref1, “do stuff” won’t print, but “do other stuff” will keep on printing on the “event”

    • return: Ref counter of the subscription. See func off()

    Declaration

    Swift

    @discardableResult
    public func delegateOn<Target: AnyObject>(_ event: String,
                                              to owner: Target,
                                              callback: @escaping ((Target, Message) -> Void)) -> Int

    Parameters

    event

    Event to receive

    owner

    Class registering the callback. Usually self

    callback

    Called with the event’s message

  • Unsubscribes from a channel event. If a ref is given, only the exact listener will be removed. Else all listeners for the event will be removed.

    Example:

    let channel = socket.channel("topic")
    let ref1 = channel.on("event") { _ in print("ref1 event" }
    let ref2 = channel.on("event") { _ in print("ref2 event" }
    let ref3 = channel.on("other_event") { _ in print("ref3 other" }
    let ref4 = channel.on("other_event") { _ in print("ref4 other" }
    channel.off("event", ref1)
    channel.off("other_event")
    

    After this, only “ref2 event” will be printed if the channel receives “event” and nothing is printed if the channel receives “other_event”.

    • paramter ref: Ref counter returned when subscribing. Can be omitted

    Declaration

    Swift

    public func off(_ event: String, ref: Int? = nil)

    Parameters

    event

    Event to unsubscribe from

  • Push a payload to the Channel

    Example:

    channel
        .push("event", payload: ["message": "hello")
        .receive("ok") { _ in { print("message sent") }
    

    Declaration

    Swift

    @discardableResult
    public func push(_ event: String,
                     payload: Payload,
                     timeout: TimeInterval = Defaults.timeoutInterval) -> Push

    Parameters

    event

    Event to push

    payload

    Payload to push

    timeout

    Optional timeout

  • Leaves the channel

    Unsubscribes from server events, and instructs channel to terminate on server

    Triggers onClose() hooks

    To receive leave acknowledgements, use the a receive hook to bind to the server ack, ie:

    Example: / channel.leave().receive(“ok”) { _ in { print(“left”) }

    • return: Push that can add receive hooks

    Declaration

    Swift

    @discardableResult
    public func leave(timeout: TimeInterval = Defaults.timeoutInterval) -> Push

    Parameters

    timeout

    Optional timeout

  • Overridable message hook. Receives all events for specialized message handling before dispatching to the channel callbacks.

    • return: Must return the payload, modified or unmodified

    Declaration

    Swift

    public func onMessage(callback: @escaping (Message) -> Message)

    Parameters

    event

    The event the message was for

    payload

    The payload for the message

    ref

    The reference of the message

Public API

    • return: True if the Channel has been closed

    Declaration

    Swift

    public var isClosed: Bool { get }
    • return: True if the Channel experienced an error

    Declaration

    Swift

    public var isErrored: Bool { get }
    • return: True if the channel has joined

    Declaration

    Swift

    public var isJoined: Bool { get }
    • return: True if the channel has requested to join

    Declaration

    Swift

    public var isJoining: Bool { get }
    • return: True if the channel has requested to leave

    Declaration

    Swift

    public var isLeaving: Bool { get }