Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!