LightstreamerClient

public class LightstreamerClient

Façade class for the management of the communication to Lightstreamer Server.

Used to provide configuration settings, event handlers, operations for the control of the connection lifecycle, subscription handling and to send messages.

  • Client status and transport (when applicable).

    See more

    Declaration

    Swift

    public enum Status : String, CustomStringConvertible
  • A status filter to unsubscribe multiple MPN subscriptions at once. E.g. by passing TRIGGERED it is possible to unsubscribe all triggered MPN subscriptions.

    See more

    Declaration

    Swift

    public enum MPNSubscriptionStatus
  • Creates an object to be configured to connect to a Lightstreamer server and to handle all the communications with it.

    Each LightstreamerClient is the entry point to connect to a Lightstreamer server, subscribe to as many items as needed and to send messages.

    Precondition

    the address must be valid. See ConnectionDetails.serverAddress for details.

    Declaration

    Swift

    public convenience init(serverAddress: String?, adapterSet: String? = nil)

    Parameters

    serverAddress

    The address of the Lightstreamer Server to which this LightstreamerClient will connect to. It is possible to specify it later by using nil here. See ConnectionDetails.serverAddress for details.

    adapterSet

    The name of the Adapter Set mounted on Lightstreamer Server to be used to handle all requests in the Session associated with this LightstreamerClient. It is possible not to specify it at all or to specify it later by using nil here. See ConnectionDetails.adapterSet for details.

  • A constant string representing the name of the library.

    Declaration

    Swift

    public static let LIB_NAME: String
  • A constant string representing the version of the library.

    Declaration

    Swift

    public static let LIB_VERSION: String
  • Static method that permits to configure the logging system used by the library.

    The logging system must respect the LSLoggerProvider interface. A custom class can be used to wrap any third-party logging system.

    If no logging system is specified, all the generated log is discarded.

    The following categories are available to be consumed:

    • lightstreamer.stream: logs socket activity on Lightstreamer Server connections;

      • at INFO level, socket operations are logged;
      • at DEBUG level, read/write data exchange is logged.
    • lightstreamer.protocol: logs requests to Lightstreamer Server and Server answers;

      • at INFO level, requests are logged;
      • at DEBUG level, request details and events from the Server are logged.
    • lightstreamer.session: logs Server Session lifecycle events;

      • at INFO level, lifecycle events are logged;
      • at DEBUG level, lifecycle event details are logged.
    • lightstreamer.subscriptions: logs subscription requests received by the clients and the related updates;

      • at WARN level, alert events from the Server are logged;
      • at INFO level, subscriptions and unsubscriptions are logged;
      • at DEBUG level, requests batching and update details are logged.
    • lightstreamer.actions: logs settings / API calls.

    • lightstreamer.transport: logs lower-level transport operations (HTTP and WebSocket).

    • lightstreamer.reachability: logs network reachability status.

    Declaration

    Swift

    public static func setLoggerProvider(_ loggerProvider: LSLoggerProvider)

    Parameters

    provider

    An LSLoggerProvider instance that will be used to generate log messages by the library classes.

  • Static method that can be used to share cookies between connections to the Server (performed by this library) and connections to other sites that are performed by the application.

    With this method, cookies received by the application can be added (or replaced if already present) to the cookie set used by the library to access the Server. Obviously, only cookies whose domain is compatible with the Server domain will be used internally.

    Specified cookies are actually added to the system’s shared cookie storage, which is used by both HTTP and WebSocket connections.

    Lifecycle: this method can be called at any time; it will affect the internal cookie set immediately and the sending of cookies on future requests.

    Declaration

    Swift

    public static func addCookies(_ cookies: [HTTPCookie], forURL url: URL)

    Parameters

    cookies

    A list of cookies, represented by the system’s standard cookie object HTTPCookie.

    url

    The URL from which the supplied cookies were received.

  • Static inquiry method that can be used to share cookies between connections to the Server (performed by this library) and connections to other sites that are performed by the application.

    With this method, cookies received from the Server can be extracted for sending through other connections, according with the URL to be accessed.

    Declaration

    Swift

    public static func getCookiesForURL(_ url: URL) -> [HTTPCookie]?

    Parameters

    url

    The URL to which the cookies should be sent, or null.

    Return Value

    An immutable list with the various cookies that can be sent in a HTTP request for the specified URL. If a null URI was supplied, all available non-expired cookies will be returned. The cookies are represented by the system’s standard cookie object HTTPCookie.

  • Object that contains the details needed to open a connection to a Lightstreamer Server.

    This instance is set up by the LightstreamerClient object at its own creation.

    Properties of this object can be overwritten by values received from a Lightstreamer Server.

    Declaration

    Swift

    public var connectionDetails: ConnectionDetails { get }
  • Object that contains options and policies for the connection to the server.

    This instance is set up by the LightstreamerClient object at its own creation.

    Properties of this object can be overwritten by values received from a Lightstreamer Server.

    Declaration

    Swift

    public var connectionOptions: ConnectionOptions { get }
  • Current client status and transport (when applicable).

    It can be one of the following values:

    • CONNECTING the client is waiting for a Server’s response in order to establish a connection;

    • CONNECTED:STREAM-SENSING the client has received a preliminary response from the server and is currently verifying if a streaming connection is possible;

    • CONNECTED:WS-STREAMING a streaming connection over WebSocket is active;

    • CONNECTED:HTTP-STREAMING a streaming connection over HTTP is active;

    • CONNECTED:WS-POLLING a polling connection over WebSocket is in progress;

    • CONNECTED:HTTP-POLLING a polling connection over HTTP is in progress;

    • STALLED the Server has not been sending data on an active streaming connection for longer than a configured time;

    • DISCONNECTED:WILL-RETRY no connection is currently active but one will be opened (possibly after a timeout);

    • DISCONNECTED:TRYING-RECOVERY no connection is currently active, but one will be opened as soon as possible, as an attempt to recover the current session after a connection issue;

    • DISCONNECTED no connection is currently active.

    Platform limitations: On watchOS the WebSocket transport is not available.

    Declaration

    Swift

    public var status: Status { get }
  • Adds a delegate that will receive events from the LightstreamerClient instance.

    The same delegate can be added to several different LightstreamerClient instances.

    Lifecycle: a delegate can be added at any time. A call to add a delegate already present will be ignored.

    Declaration

    Swift

    public func addDelegate(_ delegate: ClientDelegate)

    Parameters

    delegate

    An object that will receive the events as documented in the ClientDelegate interface. Note: delegates are stored with weak references: make sure you keep a strong reference to your delegates or they may be released prematurely.

  • Removes a delegate from the LightstreamerClient instance so that it will not receive events anymore.

    Lifecycle: a delegate can be removed at any time.

    See also

    addDelegate(_:)

    Declaration

    Swift

    public func removeDelegate(_ delegate: ClientDelegate)

    Parameters

    delegate

    The delegate to be removed.

  • List containing the ClientDelegate instances that were added to this client.

    See also

    addDelegate(_:)

    Declaration

    Swift

    public var delegates: [ClientDelegate] { get }
  • Operation method that requests to open a Session against the configured Lightstreamer Server.

    When connect is called, unless a single transport was forced through ConnectionOptions.forcedTransport, the so called “Stream-Sense” mechanism is started: if the client does not receive any answer for some seconds from the streaming connection, then it will automatically open a polling connection.

    A polling connection may also be opened if the environment is not suitable for a streaming connection.

    Note that as “polling connection” we mean a loop of polling requests, each of which requires opening a synchronous (i.e. not streaming) connection to Lightstreamer Server.

    Lifecycle: note that the request to connect is accomplished by the client in a separate thread; this means that an invocation of status right after connect might not reflect the change yet.

    When the request to connect is finally being executed, if the current status of the client is CONNECTING, CONNECTED:* or STALLED, then nothing will be done.

    Precondition

    a server address must be configured.

    See also

    status

    See also

    disconnect()

    Declaration

    Swift

    public func connect()
  • Operation method that requests to close the Session opened against the configured Lightstreamer Server (if any).

    When disconnect() is called, the “Stream-Sense” mechanism is stopped.

    Note that active Subscription instances, associated with this LightstreamerClient instance, are preserved to be re-subscribed to on future Sessions.

    Lifecycle: note that the request to disconnect is accomplished by the client in a separate thread; this means that an invocation of status right after disconnect might not reflect the change yet.

    When the request to disconnect is finally being executed, if the status of the client is DISCONNECTED, then nothing will be done.

    See also

    connect()

    Declaration

    Swift

    public func disconnect()
  • Operation method that sends a message to the Server.

    The message is interpreted and handled by the Metadata Adapter associated to the current Session. This operation supports in-order guaranteed message delivery with automatic batching. In other words, messages are guaranteed to arrive exactly once and respecting the original order, whatever is the underlying transport (HTTP or WebSockets). Furthermore, high frequency messages are automatically batched, if necessary, to reduce network round trips.

    Upon subsequent calls to the method, the sequential management of the involved messages is guaranteed. The ordering is determined by the order in which the calls to sendMessage(...) are issued. If a message, for any reason, doesn’t reach the Server (this is possible with the HTTP transport), it will be resent; however, this may cause the subsequent messages to be delayed. For this reason, each message can specify a delayTimeout, which is the longest time the message, after reaching the Server, can be kept waiting if one of more preceding messages haven’t been received yet. If the delayTimeout expires, these preceding messages will be discarded; any discarded message will be notified to the listener through ClientMessageDelegate.client(_:didDiscardMessage:).

    Note that, because of the parallel transport of the messages, if a zero or very low timeout is set for a message and the previous message was sent immediately before, it is possible that the latter gets discarded even if no communication issues occur. The Server may also enforce its own timeout on missing messages, to prevent keeping the subsequent messages for long time.

    Sequence identifiers can also be associated with the messages. In this case, the sequential management is restricted to all subsets of messages with the same sequence identifier associated.

    Notifications of the operation outcome can be received by supplying a suitable delegate. The supplied delegate is guaranteed to be eventually invoked; delegates associated with a sequence are guaranteed to be invoked sequentially.

    The UNORDERED_MESSAGES sequence name has a special meaning. For such a sequence, immediate processing is guaranteed, while strict ordering and even sequentialization of the processing is not enforced. Likewise, strict ordering of the notifications is not enforced. However, messages that, for any reason, should fail to reach the Server whereas subsequent messages had succeeded, might still be discarded after a server-side timeout, in order to ensure that the listener eventually gets a notification.

    Moreover, if UNORDERED_MESSAGES is used and no listener is supplied, a “fire and forget” scenario is assumed. In this case, no checks on missing, duplicated or overtaken messages are performed at all, so as to optimize the processing and allow the highest possible throughput.

    Lifecycle: since a message is handled by the Metadata Adapter associated to the current connection, a message can be sent only if a connection is currently active. If the special enqueueWhileDisconnected flag is specified it is possible to call the method at any time and the client will take care of sending the message as soon as a connection is available, otherwise, if the current status is DISCONNECTED*, the message will be abandoned and the ClientMessageDelegate.client(_:didAbortMessage:sentOnNetwork:) event will be fired.

    Note that, in any case, as soon as the status switches again to DISCONNECTED*, any message still pending is aborted, including messages that were queued with the enqueueWhileDisconnected flag set to true.

    Also note that forwarding of the message to the server is made in a separate thread, hence, if a message is sent while the connection is active, it could be aborted because of a subsequent disconnection. In the same way a message sent while the connection is not active might be sent because of a subsequent connection.

    Declaration

    Swift

    public func sendMessage(_ message: String, withSequence sequence: String? = nil, timeout delayTimeout: Millis = -1, delegate: ClientMessageDelegate? = nil, enqueueWhileDisconnected: Bool = false)

    Parameters

    message

    A text message, whose interpretation is entirely demanded to the Metadata Adapter associated to the current connection.

    sequence

    An alphanumeric identifier, used to identify a subset of messages to be managed in sequence; underscore characters are also allowed. If the UNORDERED_MESSAGES identifier is supplied, the message will be processed in the special way described above. The parameter is optional; if set to nil, UNORDERED_MESSAGES is used as the sequence name.

    delayTimeout

    A timeout, expressed in milliseconds. If higher than the Server configured timeout on missing messages, the latter will be used instead. The parameter is optional; if a negative value is supplied, the Server configured timeout on missing messages will be applied. This timeout is ignored for the special UNORDERED_MESSAGES sequence, although a server-side timeout on missing messages still applies.

    delegate

    An object suitable for receiving notifications about the processing outcome. The parameter is optional; if not supplied, no notification will be available. Note: delegates are stored with weak references: make sure you keep a strong reference to your delegates or they may be released prematurely.

    enqueueWhileDisconnected

    If this flag is set to true, and the client is in a disconnected status when the provided message is handled, then the message is not aborted right away but is queued waiting for a new session. Note that the message can still be aborted later when a new session is established.

  • Operation method that adds a Subscription to the list of “active” subscriptions.

    The Subscription cannot already be in the “active” state.

    Active subscriptions are subscribed to through the server as soon as possible (i.e. as soon as there is a session available). Active Subscription are automatically reissued across subsequent sessions as long as a related unsubscribe(_:) call is not issued.

    Lifecycle: an Subscription can be given to the LightstreamerClient at any time. Once done the Subscription immediately enters the “active” state.

    Once “active”, a Subscription instance cannot be provided again to a LSLightstreamerClient unless it is first removed from the “active” state through a call to unsubscribe(_:).

    Also note that forwarding of the subscription to the server is made in a separate thread.

    A successful subscription to the server will be notified through a SubscriptionDelegate.subscriptionDidSubscribe(_:) event.

    See also

    unsubscribe(_:)

    Declaration

    Swift

    public func subscribe(_ subscription: Subscription)

    Parameters

    subscription

    A Subscription object, carrying all the information needed to process real-time values.

  • Operation method that removes a Subscription that is currently in the “active” state.

    By bringing back a Subscription to the “inactive” state, the unsubscription from all its items is requested to Lightstreamer Server.

    Lifecycle: an Subscription can be unsubscribed from at any time. Once done the Subscription immediately exits the “active” state.

    Note that forwarding of the unsubscription to the server is made in a separate thread.

    The unsubscription will be notified through a SubscriptionDelegate.subscriptionDidUnsubscribe(_:) event.

    Declaration

    Swift

    public func unsubscribe(_ subscription: Subscription)

    Parameters

    subscription

    An “active” Subscription object that was activated by this LightstreamerClient instance.

  • List containing all the Subscription instances that are currently “active” on this LightstreamerClient.

    Internal second-level Subscription are not included.

    The list can be empty.

    See also

    subscribe(_:)

    Declaration

    Swift

    public var subscriptions: [Subscription] { get }
  • Operation method that registers the MPN device on the server’s MPN Module.

    By registering an MPN device, the client enables MPN functionalities such as subscribeMPN(_:coalescing:) and resetMPNBadge().

    Edition note: MPN is an optional feature, available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

    Lifecycle: an MPNDevice can be registered at any time. The registration will be notified through a MPNDeviceDelegate.mpnDeviceDidRegister(_:) event.

    Note that forwarding of the registration to the server is made in a separate thread.

    See also

    resetMPNBadge()

    Declaration

    Swift

    public func register(forMPN mpnDevice: MPNDevice)

    Parameters

    mpnDevice

    An MPNDevice object, carrying all the information about the MPN device.

  • Operation method that subscribes an MPNSubscription on server’s MPN Module.

    This operation adds the MPNSubscription to the list of “active” subscriptions. MPN subscriptions are activated on the server as soon as possible (i.e. as soon as there is a session available and subsequently as soon as the MPN device registration succeeds). Differently than real-time subscriptions, MPN subscriptions are persisted on the server’s MPN Module database and survive the session they were created on.

    If the coalescing flag is set, the activation of two MPN subscriptions with the same Adapter Set, Data Adapter, Group, Schema and trigger expression will be considered the same MPN subscription. Activating two such subscriptions will result in the second activation modifying the first MPNSubscription (that could have been issued within a previous session). If the coalescing flag is not set, two activations are always considered different MPN subscriptions, whatever the Adapter Set, Data Adapter, Group, Schema and trigger expression are set.

    The rationale behind the coalescing flag is to allow simple apps to always activate their MPN subscriptions when the app starts, without worrying if the same subscriptions have been activated before or not. In fact, since MPN subscriptions are persistent, if they are activated every time the app starts and the coalescing flag is not set, every activation is a new MPN subscription, leading to multiple push notifications for the same event.

    Edition note: MPN is an optional feature, available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

    Lifecycle: an MPNSubscription can be given to the LightstreamerClient once an MPNDevice registration has been requested. The MPNSubscription immediately enters the “active” state.

    Once “active”, an MPNSubscription instance cannot be provided again to an LightstreamerClient unless it is first removed from the “active” state through a call to unsubscribeMPN(_:).

    Note that forwarding of the subscription to the server is made in a separate thread.

    A successful subscription to the server will be notified through an MPNSubscriptionDelegate.mpnSubscriptionDidSubscribe(_:) event.

    Precondition

    the given MPN subscription must contain a field list/field schema.

    Precondition

    the given MPN subscription must contain a item list/item group.

    Precondition

    an MPN device must be registered.

    Precondition

    the given MPN subscription must be inactive.

    Declaration

    Swift

    public func subscribeMPN(_ mpnSubscription: MPNSubscription, coalescing: Bool)

    Parameters

    mpnSubscription

    An MPNSubscription object, carrying all the information to route real-time data via push notifications.

    coalescing

    A flag that specifies if the MPN subscription must coalesce with any pre-existing MPN subscription with the same Adapter Set, Data Adapter, Group, Schema and trigger expression.

  • Operation method that unsubscribes an MPNSubscription from the server’s MPN Module.

    This operation removes the MPNSubscription from the list of “active” subscriptions.

    Edition note: MPN is an optional feature, available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

    Lifecycle: an MPNSubscription can be unsubscribed from at any time. Once done the MPNSubscription immediately exits the “active” state.

    Note that forwarding of the unsubscription to the server is made in a separate thread.

    The unsubscription will be notified through an MPNSubscriptionDelegate.mpnSubscriptionDidUnsubscribe(_:) event.

    Precondition

    an MPN device must be registered.

    Declaration

    Swift

    public func unsubscribeMPN(_ mpnSubscription: MPNSubscription)

    Parameters

    mpnSubscription

    An “active” MPNSubscription object.

  • Operation method that unsubscribes all the MPN subscriptions with a specified status from the server’s MPN Module.

    By specifying a status filter it is possible to unsubscribe multiple MPN subscriptions at once. E.g. by passing TRIGGERED it is possible to unsubscribe all triggered MPN subscriptions. This operation removes the involved MPN subscriptions from the list of “active” subscriptions.

    Possible filter values are:

    • ALL or nil

    • TRIGGERED

    • SUBSCRIBED

    Edition note: MPN is an optional feature, available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

    Lifecycle: multiple unsubscription can be requested at any time. Once done the involved MPN subscriptions immediately exit the “active” state.

    Note that forwarding of the unsubscription to the server is made in a separate thread.

    The unsubscription will be notified through an MPNSubscriptionDelegate.mpnSubscriptionDidUnsubscribe(_:) event to all involved MPN subscriptions.

    Precondition

    an MPN device must be registered.

    Declaration

    Swift

    public func unsubscribeMultipleMPN(_ filter: MPNSubscriptionStatus?)

    Parameters

    filter

    A value to be used to select the MPN subscriptions to unsubscribe. If nil all existing MPN subscriptions are unsubscribed.

  • Collection of the existing MPN subscriptions.

    Objects present in this collection are of type MPNSubscription. It contains both objects created by the user, via MPNSubscription constructors, and objects created by the client, to represent pre-existing MPN subscriptions.

    Note that objects in the collection may be substitutued at any time with equivalent ones: do not rely on pointer matching, instead rely on the MPNSubscription.subscriptionId property to verify the equivalence of two MPNSubscription objects. Substitutions may happen when an MPN subscription is modified, or when it is coalesced with a pre-existing subscription.

    Edition note: MPN is an optional feature, available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

    Lifecycle: the collection is available once an MPNDevice registration has been requested, but reflects the actual server’s collection only after an MPNDeviceDelegate.mpnDeviceDidUpdateSubscriptions(_:) event has been notified.

    Precondition

    an MPN device must be registered.

    Declaration

    Swift

    public var MPNSubscriptions: [MPNSubscription] { get }
  • Inquiry method that returns a collection of the existing MPN subscription with a specified status.

    Objects returned by this method are of type MPNSubscription. Can return both objects created by the user, via MPNSubscription constructors, and objects created by the client, to represent pre-existing MPN subscriptions.

    Note that objects returned by this method may be substitutued at any time with equivalent ones: do not rely on pointer matching, instead rely on the MPNSubscription.subscriptionId property to verify the equivalence of two MPNSubscription objects. Substitutions may happen when an MPN subscription is modified, or when it is coalesced with a pre-existing subscription.

    Possible filter values are:

    • ALL or nil

    • TRIGGERED

    • SUBSCRIBED

    Edition note: MPN is an optional feature, available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

    Precondition

    an MPN device must be registered.

    See also

    MPNSubscriptions

    Declaration

    Swift

    public func filterMPNSubscriptions(_ filter: MPNSubscriptionStatus?) -> [MPNSubscription]

    Parameters

    filter

    A value to be used to select the MPN subscriptions to return. If nil all existing MPN subscriptions are returned.

  • Inquiry method that returns the MPNSubscription with the specified subscription ID, or nil if not found.

    The object returned by this method can be an object created by the user, via MPNSubscription constructors, or an object created by the client, to represent pre-existing MPN subscriptions.

    Note that objects returned by this method may be substitutued at any time with equivalent ones: do not rely on pointer matching, instead rely on the MPNSubscription.subscriptionId property to verify the equivalence of two MPNSubscription objects. Substitutions may happen when an MPN subscription is modified, or when it is coalesced with a pre-existing subscription.

    Edition note: MPN is an optional feature, available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

    Precondition

    an MPN device must be registered.

    See also

    MPNSubscriptions

    Declaration

    Swift

    public func findMPNSubscription(_ subscriptionId: String) -> MPNSubscription?

    Parameters

    subscriptionId

    The subscription ID to search for.

  • Operation method that resets the counter for the app badge.

    If the AUTO value has been used for the app badge in the MPNSubscription.notificationFormat of one or more MPN subscriptions, this operation resets the counter so that the next push notification will have badge “1”.

    Edition note: MPN is an optional feature, available depending on Edition and License Type. To know what features are enabled by your license, please see the License tab of the Monitoring Dashboard (by default, available at /dashboard).

    Precondition

    an MPN device must be registered.

    Declaration

    Swift

    public func resetMPNBadge()