Lightstreamer iOS Client  2.1.2
Native iOS Client library for Lightstreamer
Getting Started With the iOS, macOS and tvOS Client Libraries

The iOS, macOS and tvOS Client Libraries, from version 2.x on, follow the Unified Client API model: a common API model used across all Lightstreamer client libraries.
Obtaining a connection and subscribing to a table is a simple and straightforward procedure:

Note: as of version 2.1, iOS, macOS and tvOS client libraries do not contain Mobile Push Notifications (MPN) APIs. They will be added in a subsequest release of the Unified Client API model. In the meantime, you can still have full MPN functionalities by using previous client libraries: 1.4.x for iOS, 1.2.x for macOS and 1.0.x for tvOS, which are still available and supported.

Importing the Framework in Your Project

Beginning with version 2.1, the client library is packaged as a framework. This both simplifies its inclusion in your project and provides better interoperability with Swift.
To add it to your project proceed as follows:


Creating an Instance of LSLightstreamerClient

To create an instance of LSLightstreamerClient simply allocate and initialize it:

// Objective-C
LSLightstreamerClient *client= [[LSLightstreamerClient alloc] initWithServerAddress:@"http://myserver.mydomain.com" adapterSet:@"MY_ADAPTER_SET"];


With Swift:

// Swift
let client = LSLightstreamerClient(serverAddress: "http://myserver.mydomain.com", adapterSet: "MY_ADAPTER_SET")


Setting Connection Parameters and Starting the Connection

You can set additional connection parameters on LSLightstreamerClient::connectionDetails and LSLightstreamerClient::connectionOptions. E.g.:

// Objective-C
client.connectionDetails.user= @"my_user";
[client.connectionDetails setPassword:@"my_password"];
client.connectionOptions.maxBandwidth= @"100";


Before connecting you may want to add a delegate:

[client addDelegate:self];

Done this, connect using connect (LSLightstreamerClient):

[client connect];

The corresponding code with Swift is:

// Swift
client.connectionDetails.user = "my_user"
client.connectionDetails.setPassword("my_password")
client.connectionOptions.maxBandwidth = "100"

client.addDelegate(self)

client.connect()


Please note that the connect (LSLightstreamerClient) call is now asynchronous: it will not block and return immeditaly. You may safely use it on the main thread. Connection progress will be notified through delegate events.

Listening for Connection Events

Added delegates will receive the LSClientDelegate::client:didChangeStatus: event each time the connection changes its status:

// Objective-C
- (void) client:(nonnull LSLightstreamerClient *)client didChangeStatus:(nonnull NSString *)status {
  if ([status hasPrefix:@"CONNECTED:"]) {
    // ...
  }
}


With Swift:

// Swift
func client(_ client: LSLightstreamerClient, didChangeStatus status: String) {
  if (status.hasPrefix("CONNECTED")) {
    // ...
  }
}


Once the connection is established you will receive a notification with a status beginning with "CONNECTED:". See event documentation for more information.

Creating a Subscription

You don't have to wait for a connection to be established to subscribe. You may safely subscribe in any moment, the subscription will be delivered to the server as soon as a session has been created.
To create a subscription allocate and initialize an LSSubscription instance and set its properties with the desired values:

// Objective-C
LSSubscription *subscription= [[LSSubscription alloc] initWithSubscriptionMode:@"MERGE"];
subscription.items= @[@"my_item_1", @"my_item_2"];
subscription.fields= @[@"my_field_1", @"my_field_2"];
subscription.dataAdapter= @"MY_ADAPTER";
subscription.requestedSnapshot= @"yes";
[subscription addDelegate:self];


Once the subscription is set up, subscribe using subscribe: (LSLightstreamerClient):

[client subscribe:subscription];

With Swift:

// Swift
let subscription = LSSubscription(subscriptionMode: "MERGE")
subscription.items = ["my_item_1", "my_item_2"]
subscription.fields = ["my_field_1", "my_field_2"]
subscription.dataAdapter = "MY_ADAPTER"
subscription.requestedSnapshot = "yes"
subscription.addDelegate(self)

client.subscribe(subscription)


The library keeps memory of active subscriptions and automatically resubscribes them if the connection drops.

Listening for Subscription Events

Subscription delegates will receive the LSSubscriptionDelegate::subscription:didUpdateItem: event each time an update is received:

// Objective-C
- (void) subscription:(nonnull LSSubscription *)subscription didUpdateItem:(nonnull LSItemUpdate *)itemUpdate {
  NSString *value= [itemUpdate valueWithFieldName:"my_field_1"];
  // ...
}


With Swift:

// Swift
func subscription(_ subscription: LSSubscription, didUpdateItem itemUpdate: LSItemUpdate) {
  let value = itemUpdate.value(withFieldName: "my_field_1")
  // ...
}


Congratulations! Your subscription with Lightstreamer Server is now set up!

Closing the Connection

To close the connection simply call disconnect (LSLightstreamerClient):

// Objective-C
[client disconnect];



With Swift:

// Swift
client.disconnect()