'Getting 'EventSource' to work with firebase realtime in javascript

I am trying to make a code that originally worked with Arduino-Code and Javascript(Three.js), to work with Firebase and Three.js, but have run into a bit of a pickle. The code is supposed to move a square up and down based on a GY-521 sensor.

Before I switched to Firebase, EventSource was called on /events that was set in the Arduino code. Now I have no idea how to get Firebase to work like this. Is it even possible to make Evensource with Firebase this way?

Here is the code snippet from script.js with EventSource:

// Create events for the sensor readings
if (!!window.EventSource) {
      var source = new EventSource('/events');

      source.addEventListener('open', function(e) {
        console.log("Events Connected");
      }, false);

      source.addEventListener('error', function(e) {
        if (e.target.readyState != EventSource.OPEN) {
          console.log("Events Disconnected");
        }
      }, false);

      source.addEventListener('gyro_readings', function(e) {
        //console.log("gyro_readings", e.data);
        var obj = JSON.parse(e.data);
        document.getElementById("gyroX").innerHTML = obj.gyroX;
        document.getElementById("gyroY").innerHTML = obj.gyroY;
        document.getElementById("gyroZ").innerHTML = obj.gyroZ;

        // Change cube rotation after receiving the readinds

        //cube.position.x = obj.gyroY;
        //cube.position.z = obj.gyroX;
        cube.position.y = obj.gyroX;
        renderer.render(scene, camera);
      }, false);

Here is where I initialize database and use let readings to get readings from the realtime database in Firebase:

const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
let readings;

//if wanting to use gyroX from reading then type readings.gyroX, if I want to use gyroY then type readings.gyroY

//get gyroX value

const gyroxReading = ref(db, "UsersData" + "/rQmuMKv9tgewbq6m5gwH9BmxN8U2");
console.log(gyroxReading);
onValue(gyroxReading, (snapshot) => {
   readings = snapshot.val();
   console.log(readings.gyroX);
});

Also a screenshot of the database:

database screenshot



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source