TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Mar 2015 |
Maintained by Mamad Purbo.
Depends on: | |
ReactiveCocoa | >= 0 |
SocketRocket | >= 0 |
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.
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);
}];
You can chain-call settings method after initializing to specify custom settings. Following sections explain available settings.
When using SockJS server, use useSockJs
as shown below:
MMPReactiveStompClient *stompClient = [[[MMPReactiveStompClient alloc]
initWithURL:[NSURL URLWithString:@"ws://localhost:8080/stream/connect"]]
useSockJs];
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
Mamad Purbo, [email protected]
MMPReactiveStompClient is available under the MIT license. See the LICENSE file for more info.