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

走远了吗. 提交于 2019-12-12 22:16:59

问题


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 changes. I have tried connecting to the REST API supporting server sent events using both Java and JavaScript, but I can not make it work.

In Java I have written the following code:

public class FirebaseApplication {
  @Test
  public void test() throws Exception{
    Client client = ClientBuilder.newBuilder()
      .register(SseFeature.class).build();
    WebTarget webTarget = client.target(new URI(
      "http://incandescent-torch-xxxx.firebaseio.com/logs.json"));
    EventSource eventSource = new EventSource(webTarget) {
      @Override
      public void onEvent(InboundEvent inboundEvent) {
        System.out.println("Data " + inboundEvent.readData());
      }
    };
    Thread.sleep(20000);
    System.out.println("Exit");
    eventSource.close();
  }
}

I do however not receive any events even though I am in parallel with the execution of the little program.

Next I tried using a JavaScript client, but with the same result. I never receive any events.

var source = new EventSource(
  "http://incandescent-torch-xxxx.firebaseio.com/logs.json");

source.addEventListener('message', function(e) {
  console.log(e.data);
}, false);

source.addEventListener('open', function(e) {
  console.log("open");
  // Connection was opened.
}, false);

source.addEventListener('error', function(e) {
  if (e.readyState == EventSource.CLOSED) {
    console.log("close");
    // Connection was closed.
  }else{
    console.log("error");
    console.log(e);
  }

}, false);

Does anyone having a clue of what I am doing wrong ?


回答1:


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/



来源:https://stackoverflow.com/questions/28673164/firebase-server-sent-events-how-to-build-a-java-javascript-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!