Sockets

Use sockets for real time communication between your iOS app and your back end.

The iOS Apps Panel SDK provides a way to exchange data in real time with your back end. For example, this can be used to build an instant chat in your app

Integration

The class which manage that is called APSocketManager. It can be used as a singleton, or with instantiated objects.

To connect sockets, use the setup function as following :

/*!
 @abstract Setup the singleton connexion to the server
 
 @param host The address of the server (usually [YOUR_APPLICATION].socket.apnl.ws)
 @param port The port that the connexion will use (eg 1337)

 @discussion It will create a read and a write stream with CFStreamCreatePairWithSocketToHost
 */
+ (void)setup:(NSString *)host port:(UInt32)port;

APSocketManger has a delegate called APSocketManagerConnexionDelegate. It's usefull for being aware of the connection's status, and to receive data sent by the server.

/**
 * Delegate APSocketManagerConnexionDelegate
 */
@protocol APSocketManagerConnexionDelegate <NSObject>

/*!
 @abstract Called after the connexion Failed
 
 @param result Will always be nil
 */
- (void)socketConnexionFailed:(NSDictionary *)result;

/*!
 @abstract Called after the connexion Closed
 
 @param result Will always be nil
 */
- (void)socketConnexionDidClose:(NSDictionary *)result;

/*!
 @abstract Called after the connexion Succeed
 
 @param result Will always be nil
 */
- (void)socketConnexionSuccess:(NSDictionary *)result;

/*!
 @abstract Called after the server sent data
 
 @param result Data received from the server
 */
- (void)socketConnexionReceivedData:(NSDictionary *)result;

@end

Each exchange trough socket is identified by an "action". A key-value dictionary can also be joined.

/*!
 @abstract Send a message on the write stream of the singleton connexion
 
 @param action Name of the action to call
 @param param NSDictionnary to send with the action
 @param delegate APSocketManagerDelegate
 */
+ (void)sendAction:(NSString *)action withParam:(NSDictionary *)param delegate:(id)delegate;

As you can see, send action needs an APSocketManagerDelegate. It is usefull for catching socket's response. Followings methods are available :

/** 
 * Delegate APSocketManager
 */
@protocol APSocketManagerDelegate <NSObject>

/*!
 @abstract Called after the request Failed
 
 @param result Will always be nil
 
 @bug Should never be called
 */
- (void)socketFailedDownload:(NSDictionary *)result;

/*!
 @abstract Called after the request Finish
 
 @param result Return the sended_action and the result (response from the server)
 */
- (void)socketFinishedDownload:(NSDictionary *)result;

@end