An Objective-C implementation of Server-Sent Events
Simply initialise an event source and then subscribe to receive events:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:@"http://127.0.0.1:8888/"];
MSServerSentEventsSource *serverSentEventsSource = [[MSServerSentEventsSource alloc] initWithRequest:request receive:^(MSServerSentEvent *event) {
NSLog(@"received:%@",event);
}
completion:^{
NSLog(@"closed");
}
failure:^(NSError *error) {
NSLog(@"error:%@",error);
}];
[serverSentEventsSource addListenerForEvent:@"open" usingBlock:^(MSServerSentEvent *event) {
NSLog(@"received open event");
}];
[serverSentEventsSource addListenerForEvent:@"message" usingBlock:^(MSServerSentEvent *event) {
NSLog(@"received message event");
}];
MSServerSentEvents is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "MSServerSentEvents"
Simon Heys, [email protected]
MSServerSentEvents is available under the MIT license. See the LICENSE file for more info.