Skip to content
BLWebSocketsServer is a lightweight websockets server for iOS built around libwebsockets. The server suports both synchronous requests and push.
C Objective-C Ruby
Find file
Latest commit db7eddf Jun 24, 2013 @benlodotcom Move the libwebsocket_callback_on_writable function out of the pollin…
…g loop

The libwebsocket_callback_on_writable indicates that we want to be called back when the socket is writable. The next time libwebsocket_service is called, if the socket is writable, our callback function will be called.

Readme.md

Build Status

BLWebSocketsServer is a lightweight websockets server for iOS built around libwebsockets. The server suports both synchronous requests and push.

Here's how easy it is to start a Websockets server in your iOS app:

//every request made by a client will trigger the execution of this block.
[[BLWebSocketsServer sharedInstance] setHandleRequestBlock:^NSData *(NSData *data) {
  //simply echo what has been received
  return data;
}];
//Start the server
[[BLWebSocketsServer sharedInstance] startListeningOnPort:9000 withProtocolName:@"my-protocol-name" andCompletionBlock:^(NSError *error) {
    if (!error) {
        NSLog(@"Server started");
    }
    else {
        NSLog(@"%@", error);
    }
}];
//Push a message to every connected clients
[[BLWebSocketsServer sharedInstance] pushToAll:[@"pushed message" dataUsingEncoding:NSUTF8StringEncoding]];

Installation

From CocoaPods

Add pod 'BLWebSocketsServer' to your Podfile or pod 'BLWebSocketsServer', :head if you're feeling adventurous.

Manually

Important note if your project doesn't use ARC: you must add the -fobjc-arc compiler flag to BLWebSocketsServer.m in Target Settings > Build Phases > Compile Sources.

  • Copy the BLWebSocketsServer folder into your project.
  • Add libz.dylib.
  • Import BLWebSocketsServer.h

Usage

See the sample Xcode project for an example of implementation with both synchronous and asynchronous messaging.

Reference

//Access the BLWebSocketsServer singleton
[BLWebSocketsServer sharedInstance]
//To handle a request, use a block that receives as arguments the data in the request and returns the response data
typedef NSData *(^BLWebSocketsHandleRequestBlock)(NSData * requestData);
//Add the block that'll handle the request and the corresponding response with this
- (void)setHandleRequestBlock:(BLWebSocketsHandleRequestBlock)block;
//Method to start the server
- (void)startListeningOnPort:(int)port withProtocolName:(NSString *)protocolName andCompletionBlock:(void(^)(NSError *error))completionBlock;
//Get the status of the server with this
@property (atomic, assign, readonly) BOOL isRunning;
//Push data to all the connected clients
- (void)pushToAll:(NSData *)data;
//Well...method to stop the server
- (void)stopWithCompletionBlock:(void(^)())completionBlock;

Contribute

When there is a change you'd like to make (if you don't feel inspired you can check the Todo below):

Todo

  • Add the ability to listen simultaneously on multiple ports for different protocols.
  • Use dispatch sources instead of an infinite loop.
  • Add a session store.
  • Implement per user push.

Keep working on the documentation, it is a never ending task anyway ;-)

Contact

Benjamin Loulier

License

BLWebSocketsServer is available under the MIT license. See the LICENSE file for more info.

Something went wrong with that request. Please try again.