Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda