Socket.IO / Objective C Library


Description : 

Interface to communicate between Objective C and Socket.IO with the help of websockets or Long-Polling. Originally based on fpotter's socketio-cocoa it uses other libraries/classes like
  • SocketRocket Look here for further instructions how to use/install SocketRocket.

Requirements

As of version 0.4, this library requires at least OS X 10.7 or iOS 5.0. Because of this, we were able to remove the external JSON frameworks in v0.5 and only rely on iOS' own NSJSONSerialization.

Usage

The easiest way to connect to your Socket.IO / node.js server is
SocketIO *socketIO = [[SocketIO alloc] initWithDelegate:self];
[socketIO connectToHost:@"localhost" onPort:3000];
If required, additional parameters can be included in the handshake by adding an NSDictionary to the withParamsoption:
[socketIO connectToHost:@"localhost"
                 onPort:3000
             withParams:@{@"auth_token":@"1234"}];
A namespace can also be defined in the connection details:
[socketIO connectToHost:@"localhost" onPort:3000 withParams:nil withNamespace:@"/users"];
There are different methods to send data to the server
- (void) sendMessage:(NSString *)data;
- (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function;
- (void) sendJSON:(NSDictionary *)data;
- (void) sendJSON:(NSDictionary *)data withAcknowledge:(SocketIOCallback)function;
- (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data;
- (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data andAcknowledge:(SocketIOCallback)function;
So you could send a normal Message like this
[socketIO sendMessage:@"hello world"];
or an Event (including some data) like this
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"test1" forKey:@"key1"];
[dict setObject:@"test2" forKey:@"key2"];

[socketIO sendEvent:@"welcome" withData:dict];
If you want the server to acknowledge your Message/Event you would also pass a SocketIOCallback block
SocketIOCallback cb = ^(id argsData) {
    NSDictionary *response = argsData;
    // do something with response
};
[socketIO sendEvent:@"welcomeAck" withData:dict andAcknowledge:cb];
All delegate methods are optional - you could implement the following
- (void) socketIODidConnect:(SocketIO *)socket;
- (void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error;
- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet;
- (void) socketIO:(SocketIO *)socket didReceiveJSON:(SocketIOPacket *)packet;
- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet;
- (void) socketIO:(SocketIO *)socket didSendMessage:(SocketIOPacket *)packet;
- (void) socketIO:(SocketIO *)socket onError:(NSError *)error;
To process an incoming message or event just
// message delegate
- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet
{
    NSLog(@"didReceiveMessage >>> data: %@", packet.data);
}

// event delegate
- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet
{
    NSLog(@"didReceiveEvent >>> data: %@", packet.data);
}

Usage with OS X

Running the socket.io-objc library with OS X requires some minor changes:
  • you have to use the SocketRocket.framework for OSX instead of just the submodule
    see: SocketRocket's Installing OS X
    (best way I got this to work was as a subproject and I didn't have to add the "copy file" stuff)
  • when using the osx-framework, you have to fix the import-statement in SocketIOTransportWebsocket.h
// replace
#import SRWebSocket.h

// with
#import <SocketRocket/SRWebSocket.h>

Comments

  1. Unable to Solve "Obscure Connection Timeout Issue" in MongoDB? Contact to MongoDB Technical Support
    One known issue that has been represented by a portion of our customers occurs if you set up MongoDB and introduce a non-default event. Right when this is done, the correspondence port is extremely changed. We endorse using the default case for "the most straightforward strategy" anyway if that isn't doable you ought to guarantee that TCP correspondence is enabled and you have the static port set for the organization. We very prescribe you to pick Cognegic's #MongoDB_Customer_Support_USA or #Support_for_MongoDB_Database_Software to determine this above issue.
    For More Info: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete
  2. Excellent blog, informative and knowledgeable content. Thanks for sharing this blog with us.
    Mean Stack Training in Hyderabad
    Mern Stack Training in Hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

iOS Architecture

Property vs Instance Variable (iVar) in Objective-c [Small Concept but Great Understanding..]

setNeedsLayout vs layoutIfNeeded Explained