Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

MFC Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(MFC) Firebase Receive Server-Sent Events (text/event-stream)

Demonstrates how to start receiving server-sent events and update your JSON database with each event.

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkFileAccess.h>
#include <CkRest.h>
#include <CkAuthGoogle.h>
#include <CkUrl.h>
#include <CkJsonObject.h>
#include <CkStream.h>
#include <CkServerSentEvent.h>
#include <CkTask.h>

void ChilkatSample(void)
    {
    CkString strOut;

    // 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 Get Firebase Access Token from JSON Service Account Private Key for sample code.

    // Load the previously obtained Firebase access token into a string.
    CkFileAccess fac;
    const char *accessToken = fac.readEntireTextFile("qa_data/tokens/firebaseToken.txt","utf-8");
    if (fac.get_LastMethodSuccess() != true) {
        strOut.append(fac.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    CkRest rest;

    // 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.  
    bool success = rest.Connect("chilkat.firebaseio.com",443,true,true);
    if (success != true) {
        strOut.append(rest.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    CkAuthGoogle authGoogle;
    authGoogle.put_AccessToken(accessToken);
    rest.SetAuthGoogle(authGoogle);

    rest.AddHeader("Accept","text/event-stream");
    rest.AddHeader("Cache-Control","no-cache");

    const char *responseBody = rest.fullRequestNoBody("GET","/.json");

    // A 307 redirect response is expected.
    if (rest.get_ResponseStatusCode() != 307) {
        strOut.append("Unexpected response code: ");
        strOut.appendInt(rest.get_ResponseStatusCode());
        strOut.append("\r\n");
        strOut.append(responseBody);
        strOut.append("\r\n");
        strOut.append("Failed.");
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Get the redirect URL
    CkUrl *url = rest.RedirectUrl();
    if (rest.get_LastMethodSuccess() != true) {
        strOut.append(rest.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    strOut.append("redirect URL domain: ");
    strOut.append(url->host());
    strOut.append("\r\n");
    strOut.append("redirect URL path: ");
    strOut.append(url->path());
    strOut.append("\r\n");
    strOut.append("redirect URL query params: ");
    strOut.append(url->query());
    strOut.append("\r\n");
    strOut.append("redirect URL path with query params: ");
    strOut.append(url->pathWithQueryParams());
    strOut.append("\r\n");

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

    success = rest2.Connect(url->host(),443,true,true);
    if (success != true) {
        strOut.append(rest2.lastErrorText());
        strOut.append("\r\n");
        delete url;
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    rest2.AddHeader("Accept","text/event-stream");
    rest2.AddHeader("Cache-Control","no-cache");

    // Add the redirect query params to the request
    rest2.AddQueryParams(url->query());

    // In our case, we don't actually need the auth query param,
    // so remove it.
    rest2.RemoveQueryParam("auth");

    // 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) {
        strOut.append(rest2.lastErrorText());
        strOut.append("\r\n");
        delete url;
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    delete url;

    // 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) {
        strOut.append(rest2.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // 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) {
        strOut.append("Response Code: ");
        strOut.appendInt(responseStatusCode);
        strOut.append("\r\n");
        strOut.append("Response Status Text: ");
        strOut.append(rest2.responseStatusText());
        strOut.append("\r\n");
        strOut.append("Response Header: ");
        strOut.append(rest2.responseHeader());
        strOut.append("\r\n");
        responseBody = rest2.readRespBodyString();
        if (rest2.get_LastMethodSuccess() == true) {
            strOut.append("Error Response Body: ");
            strOut.append(responseBody);
            strOut.append("\r\n");
        }

        strOut.append("Failed.");
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // 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;

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

    // 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;

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

    CkTask *task = rest2.ReadRespBodyStreamAsync(eventStream,true);
    task->Run();

    // 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.

        // An alternative to writing a while loop to read the event stream
        // would be to setup some sort of timer event in your program (using whatever timer functionality
        // is provided in a programming language/environment), to periodically check the eventStream's
        // DataAvailable property and consume the incoming event.
        const char *eventStr = eventStream.readUntilMatch("\r\n\r\n");
        if (eventStream.get_LastMethodSuccess() != true) {
            strOut.append(eventStream.lastErrorText());
            strOut.append("\r\n");
            // Force the loop to exit by setting the count to a high number.
            count = 99999;
        }
        else {
            strOut.append("Event: [");
            strOut.append(eventStr);
            strOut.append("]");
            strOut.append("\r\n");

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

            }

        }

        count = count + 1;
    }

    // Make sure the background task is cancelled if still running.
    task->Cancel();

    delete task;

    // Examine the JSON database after applying events..
    jsonDb.put_EmitCompact(false);
    strOut.append("----");
    strOut.append("\r\n");
    strOut.append(jsonDb.emit());
    strOut.append("\r\n");


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.