Sample code for 30+ languages & platforms
Tcl

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

See more Firebase Examples

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

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# 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.
set fac [new_CkFileAccess]

set accessToken [CkFileAccess_readEntireTextFile $fac "qa_data/tokens/firebaseToken.txt" "utf-8"]
if {[CkFileAccess_get_LastMethodSuccess $fac] == 0} then {
    puts [CkFileAccess_lastErrorText $fac]
    delete_CkFileAccess $fac
    exit
}

set 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.  
set success [CkRest_Connect $rest "chilkat.firebaseio.com" 443 1 1]
if {$success == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkFileAccess $fac
    delete_CkRest $rest
    exit
}

set authGoogle [new_CkAuthGoogle]

CkAuthGoogle_put_AccessToken $authGoogle $accessToken
CkRest_SetAuthGoogle $rest $authGoogle

CkRest_AddHeader $rest "Accept" "text/event-stream"
CkRest_AddHeader $rest "Cache-Control" "no-cache"

set responseBody [CkRest_fullRequestNoBody $rest "GET" "/.json"]

# A 307 redirect response is expected.
if {[CkRest_get_ResponseStatusCode $rest] != 307} then {
    puts "Unexpected response code: [CkRest_get_ResponseStatusCode $rest]"
    puts "$responseBody"
    puts "Failed."
    delete_CkFileAccess $fac
    delete_CkRest $rest
    delete_CkAuthGoogle $authGoogle
    exit
}

# Get the redirect URL
set urlStr [CkRest_lastRedirectUrl $rest]
set url [new_CkUrl]

CkUrl_ParseUrl $url $urlStr

puts "redirect URL domain: [CkUrl_host $url]"
puts "redirect URL path: [CkUrl_path $url]"
puts "redirect URL query params: [CkUrl_query $url]"
puts "redirect URL path with query params: [CkUrl_pathWithQueryParams $url]"

# Our text/event-stream will be obtained from the redirect URL...
set rest2 [new_CkRest]

set success [CkRest_Connect $rest2 [CkUrl_host $url] 443 1 1]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest2]
    delete_CkFileAccess $fac
    delete_CkRest $rest
    delete_CkAuthGoogle $authGoogle
    delete_CkUrl $url
    delete_CkRest $rest2
    exit
}

CkRest_AddHeader $rest2 "Accept" "text/event-stream"
CkRest_AddHeader $rest2 "Cache-Control" "no-cache"

# Add the redirect query params to the request
CkRest_AddQueryParams $rest2 [CkUrl_query $url]

# In our case, we don't actually need the auth query param,
# so remove it.
CkRest_RemoveQueryParam $rest2 "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.)
set success [CkRest_SendReqNoBody $rest2 "GET" [CkUrl_path $url]]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest2]
    delete_CkFileAccess $fac
    delete_CkRest $rest
    delete_CkAuthGoogle $authGoogle
    delete_CkUrl $url
    delete_CkRest $rest2
    exit
}

# 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.
set responseStatusCode [CkRest_ReadResponseHeader $rest2]
if {$responseStatusCode < 0} then {
    puts [CkRest_lastErrorText $rest2]
    delete_CkFileAccess $fac
    delete_CkRest $rest
    delete_CkAuthGoogle $authGoogle
    delete_CkUrl $url
    delete_CkRest $rest2
    exit
}

# 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} then {
    puts "Response Code: $responseStatusCode"
    puts "Response Status Text: [CkRest_responseStatusText $rest2]"
    puts "Response Header: [CkRest_responseHeader $rest2]"
    set responseBody [CkRest_readRespBodyString $rest2]
    if {[CkRest_get_LastMethodSuccess $rest2] == 1} then {
        puts "Error Response Body: $responseBody"
    }

    puts "Failed."
    delete_CkFileAccess $fac
    delete_CkRest $rest
    delete_CkAuthGoogle $authGoogle
    delete_CkUrl $url
    delete_CkRest $rest2
    exit
}

# For this example, our JSON database will be empty at the beginning.
# The incoming events (put and patch) will be applied to this database.
set jsonDb [new_CkJsonObject]

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

# 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..
set eventStream [new_CkStream]

# This sse object will be used as a helper to parse the server-sent event stream.
set sse [new_CkServerSentEvent]

# task is a CkTask
set task [CkRest_ReadRespBodyStreamAsync $rest2 $eventStream 1]
CkTask_Run $task

# For this example, we'll just read a few events, and then cancel the
# async task.
set count 0
while {expr [$count < 3]  &&  [[CkTask_get_Finished $task] == 0]} {

    # 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.
    set eventStr [CkStream_readUntilMatch $eventStream "\r\n\r\n"]
    if {[CkStream_get_LastMethodSuccess $eventStream] != 1} then {
        puts [CkStream_lastErrorText $eventStream]
        # Force the loop to exit by setting the count to a high number.
        set count 99999
    }     else {
        puts "{Event: [}$eventStr]"

        # We have an event. Let's update our local copy of the JSON database.
        set success [CkServerSentEvent_LoadEvent $sse $eventStr]
        if {$success != 1} then {
            puts "Failed to load sse event: $eventStr"
        }         else {
            # Now we can easily access the event name and data, and apply it to our JSON database:
            set success [CkJsonObject_FirebaseApplyEvent $jsonDb [CkServerSentEvent_eventName $sse] [CkServerSentEvent_data $sse]]
            if {$success != 1} then {
                puts "Failed to apply event: [CkServerSentEvent_eventName $sse]: [CkServerSentEvent_data $sse]"
            }             else {
                puts "Successfully applied event: [CkServerSentEvent_eventName $sse]: [CkServerSentEvent_data $sse]"
            }

        }

    }

    set count [expr $count + 1]
}

# Make sure the background task is cancelled if still running.
CkTask_Cancel $task

delete_CkTask $task

# Examine the JSON database after applying events..
CkJsonObject_put_EmitCompact $jsonDb 0
puts "----"
puts [CkJsonObject_emit $jsonDb]

delete_CkFileAccess $fac
delete_CkRest $rest
delete_CkAuthGoogle $authGoogle
delete_CkUrl $url
delete_CkRest $rest2
delete_CkJsonObject $jsonDb
delete_CkStream $eventStream
delete_CkServerSentEvent $sse