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

137
Views
How to listen for event in callback function with socket.io?

I have a simple socket and firebase API. When a value in my Firebase database changes, I want to emit an event from my socket server.

So far I have something like:

io.on("connection", (socket) => {  

  socket.on('resource', ({ resourceId }) => {

    const ref = db.ref(`resources/${resourceId}`);    
    
    ref.on('child_changed', (snapshot) => {
      const resource = snapshot.val()
      socket.emit(resource)
    })
  })
});

However, my child_changed callback doesn't seem to get called here because I guess the listener gets destroyed after the socket.on event has finished?

In psuedo code, I want to do something like this:

// add some form of listener for resource id
addListnerSomehow();

// create firebase ref for that resourceId
const ref = db.ref(`resources/${resourceId}`); 

// emit new value via socket when resource value changes
ref.on('child_changed', (snapshot) => {
  const resource = snapshot.val()
  socket.emit(resource)
})

How can I emit an event to specific listeners inside a callback like this?

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!