Latest 0.2.1
Homepage https://github.com/mpurbo/MMPReactiveStompClient
License MIT
Platforms ios 7.0, requires ARC
Dependencies ReactiveCocoa, SocketRocket
Authors

Version
License
Platform

MMPReactiveStompClient is a reactive WebSocket/STOMP client library based on ReactiveCocoa and SocketRocket. STOMP implementation is based on StompKit.

This is a very early version and currently only supports signals for raw WebSocket, raw STOMP frame and message, and basic STOMP subscription.

Installation

MMPReactiveStompClient is available through CocoaPods, to install
it simply add the following line to your Podfile:

pod "MMPReactiveStompClient"

Usage

Following code shows how to subscribe to raw WebSocket signals:

#import "MMPReactiveStompClient.h"
#import <SocketRocket/SRWebSocket.h>
#import <ReactiveCocoa/ReactiveCocoa.h>

// connecting to a WebSocket server
stompClient = [[MMPReactiveStompClient alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:8080/stream/connect"]];

// opening the STOMP client returns a raw WebSocket signal that you can subscribe to
[[stompClient open]
    subscribeNext:^(id x) {
        if ([x class] == [SRWebSocket class]) {
            // First time connected to WebSocket, receiving SRWebSocket object
            NSLog(@"web socket connected with: %@", x);            
        } else if ([x isKindOfClass:[NSString class]]) {
            // Subsequent signals should be NSString
        }
    }
    error:^(NSError *error) {
        NSLog(@"web socket failed: %@", error);
    }
    completed:^{
        NSLog(@"web socket closed");
    }];

Following samples shows how to get STOMP frames, messages, and subscribe to a specific channel:

// subscribe to raw STOMP frames
[[_stompClient stompFrames]
    subscribeNext:^(MMPStompFrame *frame) {
        NSLog(@"STOMP frame received: command = %@", frame.command);
    }];

// subscribe to any STOMP messages
[[stompClient stompMessages]
    subscribeNext:^(MMPStompMessage *message) {
        NSLog(@"STOMP message received: body = %@", message.body);
    }];

// subscribe to a STOMP destination
[[stompClient stompMessagesFromDestination:@"/topic/test"]
    subscribeNext:^(MMPStompMessage *message) {
        NSLog(@"STOMP message received: body = %@", message.body);
    }];

Settings

You can chain-call settings method after initializing to specify custom settings. Following sections explain available settings.

SockJS

When using SockJS server, use useSockJs as shown below:

MMPReactiveStompClient *stompClient = [[[MMPReactiveStompClient alloc]
                                                                initWithURL:[NSURL URLWithString:@"ws://localhost:8080/stream/connect"]]
                                                                useSockJs];

Custom STOMP Subscription ID

By default each subscription to any STOMP destination will be given an ID with the following pattern:

sub-[running_number]

where [running_number] is automatically incremented number that starts from 0. If you need to generate a custom ID, you can implement MMPStompSubscriptionIdGenerator protocol and pass the class to subscriptionIdGenerator method as shown in the following example:

// class implementing MMPStompSubscriptionIdGenerator protocol

@interface GeocoreStompSubscriptionIdGenerator : NSObject<MMPStompSubscriptionIdGenerator>

@property (nonatomic, assign) NSUInteger counter;

@end

@implementation GeocoreStompSubscriptionIdGenerator

// implement method for generating next ID for new subscription
- (NSString *)generateId {
    return [NSString stringWithFormat:@"mycustomsubid-%lu", (unsigned long)++_counter];
}

@end

Author

Mamad Purbo, [email protected]

License

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

Latest podspec

{
    "name": "MMPReactiveStompClient",
    "version": "0.2.1",
    "summary": "A reactive WebSocket/STOMP client based on ReactiveCocoa",
    "description": "                       MMPReactiveStompClient is a reactive WebSocket/STOMP client library based on ReactiveCocoa and SocketRocket. nn                       Features:n                       * Signal for raw WebSocket.n                       * Signal for STOMP frames, messages, and subscription. n",
    "homepage": "https://github.com/mpurbo/MMPReactiveStompClient",
    "license": "MIT",
    "authors": {
        "Mamad Purbo": "[email protected]"
    },
    "source": {
        "git": "https://github.com/mpurbo/MMPReactiveStompClient.git",
        "tag": "0.2.1"
    },
    "social_media_url": "https://twitter.com/purubo",
    "platforms": {
        "ios": "7.0"
    },
    "source_files": "Classes",
    "dependencies": {
        "ReactiveCocoa": [],
        "SocketRocket": []
    },
    "requires_arc": true
}

Pin It on Pinterest

Share This