Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda