Archived Forum Post

Index of archived forum posts


(Androidâ„¢) Firebase Receive Server-Sent Events (text/event-stream)

Oct 19 '16 at 03:27

I have a serious intention to purchase Chilkat "Boundly-only" package to implement this example in my application, but just on simple testing I receive some logs that I dont understand at all. Can anybody take a look and advice me how to handle that ?

Thx in advance


public void onCreate(Bundle savedInstanceState) {

    CkGlobal glob = new CkGlobal();
    boolean success = glob.UnlockBundle("tj_gumis for 30-day trial");
    if (success != true) {



    TextView tv = new TextView(this);

    CkCrypt2 crypt = new CkCrypt2();

    // Display the version of the CkCrypt2 class


private void trigerSse(){

    //  Demonstrates how to begin receiving server-sent events, and to update
    //  your JSON database for each event.

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  This example assumes a JWT authentication token, if required, has been previously obtained.
    //  See Firebase JWT Authentication for sample code.

    boolean success;
    String authToken = "My-previously-obtained-JWT-token";

    CkRest rest = new CkRest();

    //  Make the initial connection (without sending a request yet).
    //  Once connected, any number of requests may be sent.  It is not necessary to explicitly
    //  call Connect before each request.
    success = rest.Connect("",80,true,true);
    if (success != true) {
        Log.i(TAG, rest.lastErrorText());


    String responseBody = rest.fullRequestNoBody("GET","/.json");

    //  A 307 redirect response is expected.
    if (rest.get_ResponseStatusCode() != 307) {
        Log.i(TAG, "Unexpected response code: " + String.valueOf(rest.get_ResponseStatusCode()));
        Log.i(TAG, responseBody);
        Log.i(TAG, "Failed.");

    //  Get the redirect URL
    CkUrl url = rest.RedirectUrl();
    if (rest.get_LastMethodSuccess() != true) {
        Log.i(TAG, rest.lastErrorText());

    Log.i(TAG, "redirect URL domain: " +;
    Log.i(TAG, "redirect URL path: " + url.path());
    Log.i(TAG, "redirect URL query params: " + url.query());
    Log.i(TAG, "redirect URL path with query params: " + url.pathWithQueryParams());

    //  Our text/event-stream will be obtained from the redirect URL...
    CkRest rest2 = new CkRest();

    success = rest2.Connect(,443,true,true);
    if (success != true) {
        Log.i(TAG, rest2.lastErrorText());



    //  Add the redirect query params to the request

    //  In our case, we don't actually need the auth query param,
    //  so remove it.

    //  Send the request.  (We are only sending the request here.
    //  We are not yet getting the response because the response
    //  will be a text/event-stream.)
    success = rest2.SendReqNoBody("GET",url.path());
    if (success != true) {
        Log.i(TAG, rest2.lastErrorText());


    //  Read the response header.
    //  We want to first get the response header to see if it's a successful
    //  response status code.  If not, then the response will not be a text/event-stream
    //  and we should read the response body normally.
    int responseStatusCode = rest2.ReadResponseHeader();
    if (responseStatusCode < 0) {
        Log.i(TAG, rest2.lastErrorText());

    //  If successful, a 200 response code is expected.
    //  If the reponse code is not 200, then read the response body and fail..
    if (responseStatusCode != 200) {
        Log.i(TAG, "Response Code: " + String.valueOf(responseStatusCode));
        Log.i(TAG, "Response Status Text: " + rest2.responseStatusText());
        Log.i(TAG, "Response Header: " + rest2.responseHeader());
        responseBody = rest2.readRespBodyString();
        if (rest2.get_LastMethodSuccess() == true) {
            Log.i(TAG, "Error Response Body: " + responseBody);

        Log.i(TAG, "Failed.");

    //  For this example, our JSON database will be empty at the beginning.
    //  The incoming events (put and patch) will be applied to this database.
    CkJsonObject jsonDb = new CkJsonObject();

    //  Make sure to set the JSON path delimiter to "/".  The default is "." and this
    //  is not compatible with Firebase paths.

    //  At this point, we've received the response header.  Now it's time to begin
    //  receiving the event stream.  We'll start a background thread to read the
    //  stream.  (Our main application (foreground) thread can cancel it at any time.)
    //  While receiving in the background thread, our foreground thread can read the stream
    //  as it desires..
    CkStream eventStream = new CkStream();

    //  This sse object will be used as a helper to parse the server-sent event stream.
    CkServerSentEvent sse = new CkServerSentEvent();

    CkTask task = rest2.ReadRespBodyStreamAsync(eventStream,true);

    //  For this example, we'll just read a few events, and then cancel the
    //  async task.
    int count = 0;
    while ((count < 3) && (task.get_Finished() == false)) {

        //  Get the next event, which is a series of text lines ending with
        //  a blank line.
        //  Note: This method blocks the calling thread until a message arrives.
        //  a program might instead periodically check the availability of
        //  data via the stream's DataAvailable property, and then do the read.

        //  Alternatively, to avoid polling,
        //  it is possible to receive the data in callbacks.
        //  Examples w/ callbacks are shown elsewhere.
        String eventStr = eventStream.readUntilMatch("\r\n\r\n");
        if (eventStream.get_LastMethodSuccess() != true) {
            Log.i(TAG, eventStream.lastErrorText());
            //  Force the loop to exit by setting the count to a high number.
            count = 99999;
        else {
            Log.i(TAG, "Event: [" + eventStr + "]");

            //  We have an event. Let's update our local copy of the JSON database.
            success = sse.LoadEvent(eventStr);
            if (success != true) {
                Log.i(TAG, "Failed to load sse event: " + eventStr);
            else {
                //  Now we can easily access the event name and data, and apply it to our JSON database:
                success = jsonDb.FirebaseApplyEvent(sse.eventName(),;
                if (success != true) {
                    Log.i(TAG, "Failed to apply event: " + sse.eventName() + ": " +;
                else {
                    Log.i(TAG, "Successfully applied event: " + sse.eventName() + ": " +;



        count = count + 1;

    //  Make sure the background task is cancelled if still running.

    //  Examine the JSON database after applying events..
    Log.i(TAG, "----");
    Log.i(TAG, jsonDb.emit());

static {
    // Important: Make sure the name passed to loadLibrary matches the shared library
    // found in your project's libs/armeabi directory.
    //  for "", pass "chilkat" to loadLibrary
    //  for "", pass "chilkatemail" to loadLibrary
    //  etc.

    // Note: If the incorrect library name is passed to System.loadLibrary,
    // then you will see the following error message at application startup:
    //"The application <your-application-name> has stopped unexpectedly. Please try again."