MPNSubscription

public class MPNSubscription : CustomStringConvertible

Class representing a Mobile Push Notifications (MPN) subscription to be submitted to the MPN Module of a Lightstreamer Server.

It contains subscription details and the delegate needed to monitor its status. Real-time data is routed via native push notifications.

In order to successfully subscribe an MPN subscription, first an MPNDevice must be created and registered on the LightstreamerClient with LightstreamerClient.register(forMPN:).

After creation, an MPNSubscription object is in the “inactive” state. When an MPNSubscription object is subscribed to on an LightstreamerClient object, through the LightstreamerClient.subscribeMPN(_:coalescing:) method, its state switches to “active”. This means that the subscription request is being sent to the Lightstreamer Server. Once the server accepted the request, it begins to send real-time events via native push notifications and the MPNSubscription object switches to the “subscribed” state.

If a triggerExpression is set, the MPN subscription does not send any push notifications until the expression evaluates to true. When this happens, the MPN subscription switches to “triggered” state and a single push notification is sent. Once triggered, no other push notifications are sent.

When an MPNSubscription is subscribed on the server, it acquires a permanent subscriptionId that the server later uses to identify the same MPN subscription on subsequent sessions.

An MPNSubscription can be configured to use either an Item Group or an Item List to specify the items to be subscribed to, and using either a Field Schema or Field List to specify the fields. The same rules that apply to Subscription apply to MPNSubscription.

An MPNSubscription object can also be provided by the client to represent a pre-existing MPN subscription on the server. In fact, differently than real-time subscriptions, MPN subscriptions are persisted on the server’s MPN Module database and survive the session they were created on.

MPN subscriptions are associated with the MPN device, and after the device has been registered the client retrieves pre-existing MPN subscriptions from the server’s database and exposes them in the LightstreamerClient.MPNSubscriptions collection.

  • The mode of a Subscription.

    See also

    mode
    See more

    Declaration

    Swift

    public enum Mode : String, CustomStringConvertible
  • Length to be requested to Lightstreamer Server for the internal queuing buffers for the items in a Subscription.

    See more

    Declaration

    Swift

    public enum RequestedBufferSize : Equatable, CustomStringConvertible
  • Maximum update frequency to be requested to Lightstreamer Server for all the items in a Subscription.

    See more

    Declaration

    Swift

    public enum RequestedMaxFrequency : Equatable, CustomStringConvertible
  • The status of a subscription.

    See more

    Declaration

    Swift

    public enum Status : String
  • Creates an object to be used to describe an MPN subscription that is going to be subscribed to through the MPN Module of Lightstreamer Server.

    The object can be supplied to LightstreamerClient.subscribeMPN(_:coalescing:) in order to bring the MPN subscription to “active” state.

    Note that all of the methods used to describe the subscription to the server, except triggerExpression and notificationFormat, can only be called while the instance is in the “inactive” state.

    Permitted values for subscription mode are:

    • MERGE

    • DISTINCT

    Declaration

    Swift

    public init(subscriptionMode: Mode)

    Parameters

    subscriptionMode

    The subscription mode for the items, required by Lightstreamer Server.

  • Creates an object to be used to describe an MPN subscription that is going to be subscribed to through the MPN Module of Lightstreamer Server.

    The object can be supplied to LightstreamerClient.subscribeMPN(_:coalescing:) in order to bring the MPN subscription to “active” state.

    Note that all of the methods used to describe the subscription to the server, except triggerExpression and notificationFormat, can only be called while the instance is in the “inactive” state.

    Permitted values for subscription mode are:

    • MERGE

    • DISTINCT

    Precondition

    the specified “Item List” and “Field List” must be valid; see items and fields for details.

    Declaration

    Swift

    public convenience init(subscriptionMode: Mode, item: String, fields: [String])

    Parameters

    subscriptionMode

    The subscription mode for the items, required by Lightstreamer Server.

    item

    The item name to be subscribed to through Lightstreamer Server.

    fields

    An array of fields for the items to be subscribed to through Lightstreamer Server. It is also possible to specify the “Field List” or “Field Schema” later through fields and fieldSchema.

  • Creates an object to be used to describe an MPN subscription that is going to be subscribed to through the MPN Module of Lightstreamer Server.

    The object can be supplied to LightstreamerClient.subscribeMPN(_:coalescing:) in order to bring the MPN subscription to “active” state.

    Note that all of the methods used to describe the subscription to the server, except triggerExpression and notificationFormat, can only be called while the instance is in the “inactive” state.

    Permitted values for subscription mode are:

    • MERGE

    • DISTINCT

    Precondition

    the specified “Item List” and “Field List” must be valid; see items and fields for details.

    Declaration

    Swift

    public convenience init(subscriptionMode: Mode, items: [String], fields: [String])

    Parameters

    subscriptionMode

    The subscription mode for the items, required by Lightstreamer Server.

    items

    An array of items to be subscribed to through Lightstreamer Server. It is also possible specify the “Item List” or “Item Group” later through items and itemGroup.

    fields

    An array of fields for the items to be subscribed to through Lightstreamer Server. It is also possible to specify the “Field List” or “Field Schema” later through fields and fieldSchema.

  • Creates an MPNSubscription object copying subscription mode, items, fields and data adapter from the specified real-time subscription.

    The object can be supplied to LightstreamerClient.subscribeMPN(_:coalescing:) in order to bring the MPN subscription to “active” state.

    Note that all of the methods used to describe the subscription to the server, except triggerExpression and notificationFormat, can only be called while the instance is in the “inactive” state.

    Declaration

    Swift

    public init(subscription: Subscription)

    Parameters

    subscription

    The Subscription object to copy properties from.

  • Creates an MPNSubscription object copying all properties from the specified MPN subscription.

    The object can be supplied to LightstreamerClient.subscribeMPN(_:coalescing:) in order to bring the MPN subscription to “active” state.

    Note that all of the methods used to describe the subscription to the server, except triggerExpression and notificationFormat, can only be called while the instance is in the “inactive” state.

    Declaration

    Swift

    public init(MPNSubscription mpnSubscription: MPNSubscription)

    Parameters

    mpnSubscription

    The MPNSubscription object to copy properties from.

  • The mode specified for this MPNSubscription.

    Lifecycle: this property can be read at any time.

    Declaration

    Swift

    public var mode: Mode { get }
  • A boolean expression that, when set, is evaluated against each update and will act as a trigger to deliver the push notification.

    If a trigger expression is set, the MPN subscription does not send any push notifications until the expression evaluates to true. When this happens, the MPN subscription “triggers” and a single push notification is sent. Once triggered, no other push notifications are sent. In other words, with a trigger expression set, the MPN subscription sends at most one push notification.

    The expression must be strictly in Java syntax and can contain named arguments with the format ${field}, or indexed arguments with the format $[1]. The same rules that apply to notificationFormat apply also to the trigger expression. The expression is verified and evaluated on the server.

    Named and indexed arguments are replaced by the server with the value of corresponding subscription fields before the expression is evaluated. They are represented as String variables, and as such appropriate type conversion must be considered. E.g.

    • Double.parseDouble(${last_price}) > 500.0

    Argument variables are named with the prefix LS_MPN_field followed by an index. Thus, variable names like LS_MPN_field1 should be considered reserved and their use avoided in the expression.

    Consider potential impact on server performance when writing trigger expressions. Since Java code may use classes and methods of the JDK, a badly written trigger may cause CPU hogging or memory exhaustion. For this reason, a server-side filter may be applied to refuse poorly written (or even maliciously crafted) trigger expressions. See the “General Concepts” document for more information.

    Remark

    if the MPNSubscription has been created by the client, such as when obtained through LightstreamerClient.MPNSubscriptions, named arguments are always mapped to its corresponding indexed argument, even if originally the trigger expression used a named argument.

    Remark

    the content of this property may be subject to length restrictions (See the “General Concepts” document for more information).

    Lifecycle: this property can be changed at any time.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument trigger on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    See also

    isTriggered

    Declaration

    Swift

    public var triggerExpression: String? { get set }

    Parameters

    newValue

    the boolean expression that acts as a trigger to deliver the push notification. If the value is nil, no trigger is set on the subscription.

    Return Value

    if the MPNSubscription is in “inactive” state, returns the trigger expression requested by the user. If the state is “active”, returns the real trigger sent by the Server or nil if the value is not available.

  • JSON structure to be used as the format of push notifications.

    This JSON structure is sent by the server to the push notification service provider (i.e. Apple’s APNs), hence it must follow its specifications.

    The JSON structure may contain named arguments with the format ${field}, or indexed arguments with the format $[1]. These arguments are replaced by the server with the value of corresponding subscription fields before the push notification is sent.

    For instance, if the subscription contains fields “stock_name” and “last_price”, the notification format could be something like this:

    • { "aps" : { "alert" : "Stock ${stock_name} is now valued ${last_price}" } }

    Named arguments are available if the Metadata Adapter is a subclass of LiteralBasedProvider or provides equivalent functionality, otherwise only indexed arguments may be used. In both cases common metadata rules apply: field names and indexes are checked against the Metadata Adapter, hence they must be consistent with the schema and group specified.

    Some special server-managed arguments may also be used:

    • AUTO: when specified for the badge value of the push notification, the badge will be assigned automatically as a progressive counter of all push notifications originated by MPN subscriptions with the “AUTO” value, on a per-device and per-application basis. The counter can also be reset at any time by calling LightstreamerClient.resetMPNBadge.

    • ${LS_MPN_subscription_ID}: the ID of the MPN subscription generating the push notification.

    The MPNBuilder object provides methods to build an appropriate JSON structure from its defining fields.

    Remark

    if the MPNSubscription has been created by the client, such as when obtained through LightstreamerClient.MPNSubscriptions, named arguments are always mapped to its corresponding indexed argument, even if originally the notification format used a named argument.

    Remark

    the content of this property may be subject to length restrictions (See the “General Concepts” document for more information).

    Lifecycle: this property can be changed at any time.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument notification_format on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    See also

    MPNBuilder

    Declaration

    Swift

    public var notificationFormat: String? { get set }

    Parameters

    newValue

    the JSON structure to be used as the format of push notifications.

    Return Value

    if the MPNSubscription is in “inactive” state, returns the notification format requested by the user. If the state is “active”, returns the real notification format sent by the Server or nil if the value is not available.

  • Name of the Data Adapter (within the Adapter Set used by the current session) that supplies all the items for this MPNSubscription.

    The Data Adapter name is configured on the server side through the “name” attribute of the <data_provider> element, in the adapters.xml file that defines the Adapter Set (a missing attribute configures the DEFAULT name).

    Note that if more than one Data Adapter is needed to supply all the items in a set of items, then it is not possible to group all the items of the set in a single MPNSubscription. Multiple MPNSubscriptions have to be defined.

    Default: the default Data Adapter for the Adapter Set, configured as DEFAULT on the Server.

    Lifecycle: this property can only be set while the MPNSubscription instance is in its “inactive” state.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument adapter on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    Precondition

    the MPNSubscription must be currently “inactive”.

    Declaration

    Swift

    public var dataAdapter: String? { get set }
  • The “Field List” to be subscribed to through Lightstreamer Server.

    Any change to this property will override any “Field List” or “Field Schema” previously specified.

    Note: if the MPNSubscription has been created by the client, such as when obtained through LightstreamerClient.MPNSubscriptions, fields are always expressed with a “Field Schema”“, even if originally the MPN subscription used a "Field List”.

    Lifecycle: this property can only be set while the MPNSubscription instance is in its “inactive” state.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument schema on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    Precondition

    the field names in the list must not contain a space or be empty/nil.

    Precondition

    the MPNSubscription must be currently “inactive”.

    Declaration

    Swift

    public var fields: [String]? { get set }
  • The “Field Schema” to be subscribed to through Lightstreamer Server.

    Any change to this property will override any “Field List” or “Field Schema” previously specified.

    Note: if the MPNSubscription has been created by the client, such as when obtained through LightstreamerClient.MPNSubscriptions, fields are always expressed with a “Field Schema”“, even if originally the MPN subscription used a "Field List”.

    Lifecycle: this property can only be set while the MPNSubscription instance is in its “inactive” state.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument schema on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    Precondition

    the MPNSubscription must be currently “inactive”.

    Declaration

    Swift

    public var fieldSchema: String? { get set }
  • The “Item Group” to be subscribed to through Lightstreamer Server.

    Any change to this property will override any “Item List” or “Item Group” previously specified.

    Note: if the MPNSubscription has been created by the client, such as when obtained through LightstreamerClient.MPNSubscriptions, items are always expressed with an “Item Group”“, even if originally the MPN subscription used an "Item List”.

    Lifecycle: this property can only be set while the MPNSubscription instance is in its “inactive” state.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument group on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    Precondition

    the MPNSubscription must be currently “inactive”.

    Declaration

    Swift

    public var itemGroup: String? { get set }
  • The “Item List” to be subscribed to through Lightstreamer Server.

    Any change to this property will override any “Item List” or “Item Group” previously specified.

    Note: if the MPNSubscription has been created by the client, such as when obtained through LightstreamerClient.MPNSubscriptions, items are always expressed with an “Item Group”“, even if originally the MPN subscription used an "Item List”.

    Lifecycle: this property can only be set while the MPNSubscription instance is in its “inactive” state.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument group on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    Precondition

    the item names in the “Item List” must not contain a space or be a number or be empty/nil.

    Precondition

    the MPNSubscription must be currently “inactive”.

    Declaration

    Swift

    public var items: [String]? { get set }
  • Length to be requested to Lightstreamer Server for the internal queuing buffers for the items in the MPNSubscription.

    A Queuing buffer is used by the Server to accumulate a burst of updates for an item, so that they can all sent as push notifications, despite of frequency limits. If the value unlimited is supplied, then the buffer length is decided by the Server.

    Note that the Server may pose an upper limit on the size of its internal buffers.

    Format: an integer number (e.g. 10), or unlimited, or nil.

    Default: nil, meaning to lean on the Server default based on the MPNSubscription mode. This means that the buffer size will be 1 for MERGE subscriptions and unlimited for DISTINCT subscriptions. See the “General Concepts” document for further details.

    Lifecycle: this property can only be set while the MPNSubscription instance is in its “inactive” state.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument requested_buffer_size on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    Precondition

    the MPNSubscription must be currently “inactive”.

    Declaration

    Swift

    public var requestedBufferSize: RequestedBufferSize? { get set }
  • Maximum update frequency to be requested to Lightstreamer Server for all the items in the MPNSubscription.

    The maximum update frequency is expressed in updates per second and applies for each item in the MPNSubscription; for instance, with a setting of 0.5, for each single item, no more than one update every 2 seconds will be received. If the value unlimited is supplied, then no frequency limit is requested. It is also possible to supply the nil value stick to the Server default (which currently corresponds to unlimited).

    Note that frequency limits on the items can also be set on the server side and this request can only be issued in order to further reduce the frequency, not to rise it beyond these limits.

    Edition note: a further global frequency limit could also be imposed by the Server, 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).

    Format: a decimal number (e.g. 2.0), or unlimited, or nil.

    Default: nil, meaning to lean on the Server default based on the MPNSubscription . This consists, for all modes, in not applying any frequency limit to the subscription (the same as unlimited); see the “General Concepts” document for further details.

    Lifecycle: this property can only be set while the MPNSubscription instance is in its “inactive” state.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument requested_max_frequency on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    Precondition

    the MPNSubscription must be currently “inactive”.

    Declaration

    Swift

    public var requestedMaxFrequency: RequestedMaxFrequency? { get set }
  • Checks if the MPNSubscription is currently “active” or not.

    Most of the MPNSubscription properties cannot be modified if an MPNSubscription is “active”.

    The status of an MPNSubscription is changed to “active” through the LightstreamerClient.subscribeMPN(_:coalescing:) method and back to “inactive” through the LightstreamerClient.unsubscribeMPN(_:) and LightstreamerClient.unsubscribeMultipleMPN(_:) ones.

    Lifecycle: this property can be read at any time.

    See also

    status

    Declaration

    Swift

    public var isActive: Bool { get }
  • Checks if the MPNSubscription is currently subscribed to through the server or not.

    This flag is switched to true by server sent subscription events, and back to false in case of client disconnection, LightstreamerClient.unsubscribeMPN(_:) or LightstreamerClient.unsubscribeMultipleMPN(_:) calls, and server sent unsubscription events.

    Lifecycle: this property can be read at any time.

    See also

    status

    Declaration

    Swift

    public var isSubscribed: Bool { get }
  • Checks if the MPNSubscription is currently triggered or not.

    This flag is switched to true when a trigger expression has been set and it evaluated to true at least once. For this to happen, the subscription must already be in isActive and isSubscribed states. It is switched back to false if the subscription is modified with a LightstreamerClient.subscribeMPN(_:coalescing:) call on a copy of it, deleted with LightstreamerClient.unsubscribeMPN(_:) or LightstreamerClient.unsubscribeMultipleMPN(_:) calls, and server sent subscription events.

    Lifecycle: this property can be read at any time.

    See also

    status

    Declaration

    Swift

    public var isTriggered: Bool { get }
  • The status of the subscription.

    The status can be:

    • UNKNOWN: when the MPN subscription has just been created or deleted (i.e. unsubscribed). In this status isActive, isSubscribed and isTriggered are all false.

    • ACTIVE: when the MPN susbcription has been submitted to the server, but no confirm has been received yet. In this status isActive is true, isSubscribed and isTriggered are false.

    • SUBSCRIBED: when the MPN subscription has been successfully subscribed on the server. If a trigger expression is set, it has not been evaluated to true yet. In this status isActive and isSubscribed are true, isTriggered is false.

    • TRIGGERED: when the MPN subscription has a trigger expression set, has been successfully on the server and the trigger expression evaluated to true at least once. In this status isActive, isSubscribed and isTriggered are all true.

    Lifecycle: this property can be read at any time.

    See also

    isActive

    See also

    isSubscribed

    See also

    isTriggered

    Declaration

    Swift

    public var status: Status { get }
  • The server-side timestamp of the subscription status.

    Lifecycle: this property can be read at any time.

    Related notifications: a change to this setting will be notified through a call to MPNSubscriptionDelegate.mpnSubscription(_:didChangeProperty:) with argument status_timestamp on any MPNSubscriptionDelegate listening to the related MPNSubscription.

    See also

    status

    Declaration

    Swift

    public var statusTimestamp: Int64 { get }
  • The server-side unique persistent ID of the MPN subscription.

    The ID is available only after the MPN subscription has been successfully subscribed on the server. I.e. when its status is SUBSCRIBED or TRIGGERED.

    Note: more than one MPNSubscription may exists at any given time referring to the same MPN subscription, and thus with the same subscription ID. For instace, copying an MPNSubscription with the copy initializer creates a second MPNSubscription instance with the same subscription ID. Also, the coalescing flag of LightstreamerClient.subscribeMPN(_:coalescing:) may cause the assignment of a pre-existing MPN subscription ID to the new subscription.

    Two MPNSubscription objects with the same subscription ID always represent the same server-side MPN subscription. It is the client’s duty to keep the status and properties of these objects up to date and aligned.

    Lifecycle: this property can be read at any time.

    Declaration

    Swift

    public var subscriptionId: String? { get }
  • Adds a delegate that will receive events from the MPNSubscription instance.

    The same delegate can be added to several different MPNSubscription 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: MPNSubscriptionDelegate)

    Parameters

    delegate

    An object that will receive the events as documented in the MPNSubscriptionDelegate 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 MPNSubscription 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: MPNSubscriptionDelegate)

    Parameters

    delegate

    The delegate to be removed.

  • List containing the MPNSubscriptionDelegate instances that were added to this MPNSubscription.

    See also

    addDelegate(_:)

    Declaration

    Swift

    public var delegates: [MPNSubscriptionDelegate] { get }
  • Declaration

    Swift

    public var description: String { get }