Firebase Server Sent Events - How to build a Java / JavaScript client

前端 未结 1 1377
离开以前
离开以前 2021-01-22 19:15

I am new to Firebase and building a prototype to test if suitable for our needs. I\'ve got a chat example up and running, so far so good.

Next is the need to listen for

相关标签:
1条回答
  • 2021-01-22 20:09

    In order to receive events, you need to listen into 'Firebase' provided events which are put, patch, keep-alive etc... I have tried and it works.

    You can refer following code snippet,

       if (typeof (EventSource) !== "undefined") {
                var source = new EventSource("https://xxxxxxxxx.firebaseio.com/TestNode.json");
                source.onmessage = function (event) {
                    document.getElementById("result").innerHTML += event.data + "<br>";
                };
    
                source.addEventListener("message", function (e) {
                    console.log(e.data);
                }, false);
    
                source.addEventListener("open", function (e) {
                    console.log("Connection was opened.");
                }, false);
    
                source.addEventListener("error", function (e) {
                    console.log("Error - connection was lost.");
                }, false);
    
                //magic goes here!
                source.addEventListener("patch", function (e) {
                    console.log("Patch UP - " + e.data);
                }, false);
                //And here!
                source.addEventListener("put", function (e) {
                    console.log("Put UP - " + e.data);
                }, false);
    
    
            } else {
                document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
            }
    

    Refer following documents, https://firebase.google.com/docs/database/rest/retrieve-data

    https://firebase.google.com/docs/reference/rest/database/

    0 讨论(0)
提交回复
热议问题