Faye Client for iOS. This project is a rewritten version of the library made by pcrawfor there: https://github.com/pcrawfor/FayeObjC Improved capture errors, added subscription blocks, ability to set the extension for channel. Added auto connection reconnect, and many more...
self.client = [[MZFayeClient alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:9292/faye"]];
[self.client subscribeToChannel:@"/server" success:^{
NSLog(@"Subscribed successfully to 'server' channel!");
} failure:^(NSError *error) {
NSLog(@"Error subscribing to 'server' channel: %@", error.userInfo);
} receivedMessage:^(NSDictionary *message) {
NSLog(@"Message on 'server' channel: %@", message);
}];
[self.client subscribeToChannel:@"/browser" success:nil failure:nil receivedMessage:^(NSDictionary *message) {
NSLog(@"Message on 'browser' channel: %@", message);
}];
[self.client connect:^{
[self.client sendMessage:@{@"text": @"hello!"} toChannel:@"/server" success:^{
NSLog(@"Message sent successfully.");
} failure:^(NSError *error) {
NSLog(@"Error sending message: %@", error.userInfo);
}];
} failure:^(NSError *error) {
NSLog(@"Error connecting: %@", error.userInfo);
}];
- (instancetype)initWithURL:(NSURL *)url;
+ (instancetype)clientWithURL:(NSURL *)url;
- (void)sendMessage:(NSDictionary *)message toChannel:(NSString *)channel success:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
- (void)sendMessage:(NSDictionary *)message toChannel:(NSString *)channel usingExtension:(NSDictionary *)extension success:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
- (void)subscribeToChannel:(NSString *)channel success:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler receivedMessage:(MZFayeClientSubscriptionHandler)subscriptionHandler;
- (void)unsubscribeFromChannel:(NSString *)channel success:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
- (void)connect:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
- (void)disconnect:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
For more info about extensions, see the Faye site,
- (void)setExtension:(NSDictionary *)extension forChannel:(NSString *)channel;
- (void)removeExtensionForChannel:(NSString *)channel;
Many delegate methods can be accomplished with blocks instead, but they still exist for those who prefer this way.
Note: In -connect:failure:
, the successHandler
and failureHandlers
will only be called the first time the connection succeeds or fails. Due to automatic reconnection logic, such events may happen multiple times. If you need to handle connection success or failure every time it happens, you should use the delegate methods instead (or in addition).
@protocol MZFayeClientDelegate <NSObject>
@optional
- (void)fayeClient:(MZFayeClient *)client didConnectToURL:(NSURL *)url;
- (void)fayeClient:(MZFayeClient *)client didDisconnectWithError:(NSError *)error;
- (void)fayeClient:(MZFayeClient *)client didUnsubscribeFromChannel:(NSString *)channel;
- (void)fayeClient:(MZFayeClient *)client didSubscribeToChannel:(NSString *)channel;
- (void)fayeClient:(MZFayeClient *)client didFailWithError:(NSError *)error;
- (void)fayeClient:(MZFayeClient *)client didFailDeserializeMessage:(NSDictionary *)message
withError:(NSError *)error;
- (void)fayeClient:(MZFayeClient *)client didReceiveMessage:(NSDictionary *)messageData fromChannel:(NSString *)channel;
@end
If you can to run a Faye server to test the client, you can find all the information you need there: http://faye.jcoglan.com/
You can also run the faye.rb
file under the Server
directory:
gem install faye thin eventmachine
rackup faye.ru -s thin -E production
curl http://localhost:9292/faye -d 'message={"channel":"/server", "data":"hello"}'
A conforming WebSocket (RFC 6455) client library maintained by Square, https://github.com/square/SocketRocket
Objective-C Base64 Additions for NSData and NSString https://github.com/ekscrypto/Base64
MZFayeClient requires either iOS 6.x and above.
MZFayeClient uses ARC.