Coder Social home page Coder Social logo

trvseventsource's Introduction

TRVSEventSource

Server-sent events EventSource implementation in ObjC for iOS and OS X using NSURLSession.

Usage

TRVSEventSource *eventSource = [[TRVSEventSource alloc] initWithURL:URL];
eventSource.delegate = self;

[eventSource addListenerForEvent:@"message" usingEventHandler:^(TRVSServerSentEvent *event, NSError *error) {
    NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:event.data options:0 error:NULL];
    Message *message = [Message messageWithJSON:JSON];
}];

[eventSource open];

Local test server

Run the following to have a local server streaming events named message:

node TRVSEventSourceTests/server.js

❯ curl 127.0.0.1:8000
event: message
data: {"id": 1, "body":"1381466575460", "author_id": 1, "conversation_id": 1}

event: message
data: {"id": 2, "body":"1381466577463", "author_id": 1, "conversation_id": 1}

trvseventsource's People

Contributors

archer-fengjiang avatar heyzooi avatar packagethief avatar travisjeffery avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

trvseventsource's Issues

Update Cocoapod to the latest

Thanks for creating this library!

It'd be awesome if you can update the cocoa pod to use the latest in the master branch.

Incompatibility with W3C specification

Specification can be found here: http://www.w3.org/TR/eventsource/

Most importantly the library ignores empty strings that are delimiters between messages.

TRVSEventSource merges everything that it finds into one message.
So next three server events will produce one TRVSServerSentEvent:

data: This is the first message.

data: This is the second message, it
data: has two lines.

data: This is the third message.

There are lot more incompatibilities anyway.

Bad access on [stream propertyForKey:]

I get data from event source for few minute and this error occur.

#pragma mark - NSStreamDelegate

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
    switch (eventCode) {
        case NSStreamEventHasSpaceAvailable: {
            //bad access on this line
            NSData *data = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
            NSError *error = nil;
            TRVSServerSentEvent *event = [TRVSServerSentEvent eventWithFields:TRVSServerSentEventFieldsFromData([data subdataWithRange:NSMakeRange(self.offset, [data length] - self.offset)], &error)];
            self.offset = [data length];

            if (error) {
                [self transitionToFailedWithError:error];
            }
            else {
                if (event) {
                    [[self.listenersKeyedByEvent objectForKey:event.event] enumerateKeysAndObjectsUsingBlock:^(id _, TRVSEventSourceEventHandler eventHandler, BOOL *stop) {
                        eventHandler(event, nil);
                    }];

                    if ([self.delegate respondsToSelector:@selector(eventSource:didReceiveEvent:)]) {
                        [self.delegate eventSource:self didReceiveEvent:event];
                    }
                }
            }
            break;
        }
        case NSStreamEventErrorOccurred: {
            [self transitionToFailedWithError:self.outputStream.streamError];
            break;
        }
        default: break;
    }
}

screen shot 2558-01-30 at 2 06 50 pm

Return http status code if event source failed

We are using this client for long running streaming data. (2000 messages per sec). And a lot of times event source failed with error set to be nil. Sometimes it is due to authentication failure to establish streaming. Sometime server restarts and connection is broken. Sometimes it is due to time out. Sometimes server closes the connection with a http status code.

So a lot of time we want to know what is causing the failure. So that we can either reconnect, or re-authenticate, or just let it fail. It would be super nice to have http status code returned in the failure callback so we have more info to determine what is going wrong.

Max number of connections

Hi,

I am curious how many concurrent clients you were able to connect to your server. I wrote a express.js (Node) server supporting event source streams and could get up to 3000 clients without optimizations, which seems not that high to me. Also the CPU is maxed out with 3000 clients.

Did you benchmarked yours? How does it perform?

Delegate methods are not called at the right time

The delegate's -eventSourceDidOpen: method is called when -open: is called on the event source, not when the connection is actually established, and is called even if the connection fails.

Similarly, -eventSourceDidClose: is called only when -close: is called on the event source, not when the connection actually closes, and not if there is a network error or the remote host unexpectedly closes the connection.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.