Created
June 10, 2015 15:46
-
-
Save jordanbyron/cf5d86e7a70ba545accd to your computer and use it in GitHub Desktop.
Server-Sent Events for React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RCTBridge.h" | |
#import "RCTConvert.h" | |
#import "RCTEventDispatcher.h" | |
#import "EventSource/EventSource.h" | |
#import "EventSourceClient.h" | |
@implementation EventSourceClient | |
@synthesize eventSource; | |
@synthesize bridge = _bridge; | |
RCT_EXPORT_MODULE(); | |
- (void)dealloc{ | |
[self.eventSource close]; | |
} | |
RCT_EXPORT_METHOD(connectWithURL:(NSString *)URLString){ | |
NSURL *serverURL = [NSURL URLWithString:URLString]; | |
self.eventSource = [EventSource eventSourceWithURL:serverURL]; | |
[self.eventSource onOpen: ^(Event *e) { | |
RCTLogInfo(@"EventSource: Connected"); | |
[self.bridge.eventDispatcher sendDeviceEventWithName:@"EventSourceConnected" | |
body:@{@"event": @"connected", | |
// Guard against null values as NSMutableDictionary | |
// expects objects | |
@"data": e.data ? e.data : [NSNull null]}]; | |
}]; | |
[self.eventSource onError: ^(Event *e) { | |
RCTLogInfo(@"EventSource: Error %@: %@", e.event, e.data); | |
[self.bridge.eventDispatcher sendDeviceEventWithName:@"EventSourceError" | |
body:@{@"event": @"error", | |
// Guard against null values as NSMutableDictionary | |
// expects objects | |
@"data": e.data ? e.data : [NSNull null]}]; | |
}]; | |
[self.eventSource onMessage: ^(Event *e) { | |
RCTLogInfo(@"%@: %@", e.event, e.data); | |
[self.bridge.eventDispatcher sendDeviceEventWithName:@"EventSourceMessage" | |
body:@{@"event": e.event, | |
// Guard against null values as NSMutableDictionary | |
// expects objects | |
@"data": e.data ? e.data : [NSNull null]}]; | |
}]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RCTBridgeModule.h" | |
#import "EventSource/EventSource.h" | |
@interface EventSourceClient : NSObject <RCTBridgeModule> { | |
EventSource *eventSource; | |
} | |
@property (nonatomic, retain) EventSource *eventSource; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react-native'); | |
var { | |
AppRegistry, | |
StyleSheet, | |
Text, | |
View, | |
TouchableHighlight, | |
AlertIOS, | |
VibrationIOS, | |
DeviceEventEmitter, | |
} = React; | |
var EventSource = require('NativeModules').EventSourceClient; | |
var MyFancyApp = React.createClass({ | |
getDefaultProps: function() { | |
return { | |
url: "https://sse/url" | |
}; | |
}, | |
componentDidMount: function() { | |
var subscription = DeviceEventEmitter.addListener( | |
'EventSourceMessage', function(message) { | |
console.log(message.event); | |
}); | |
EventSource.connectWithURL(this.props.url); | |
}, | |
componentDidUmnount: function() { | |
subscription.remove(); | |
}, | |
render: function() { | |
return (<View></View>) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment