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

226
Vistas
How to get realtime online users update using Mongoose and socket.io

I am designing an app that has many topics and for every topic there are some online users and for the same I am using express/socket.io/mongoose as backend and flutter as front end.

I am stuck at a part where I can show users that how many user is currently online for a topic in realtime(shown as list view in the frontend).

Topics are stored in the database.

So for I came up with this bit of logic but I think this is not the right way of doing it.

const roomChangeStream = roomModel.watch();

roomChangeStream.on('change', async (change) => { 
topicModel.find().then((topics) => {
topics.forEach(topic => {
  roomModel.find().where('isSearching').equals(true)
     .where('preferredTopic').equals(topic.slug).count( (err,count)=>{
    if (err) console.error(err);
     console.log("the count for" +topic.slug+" is ",count)
     // socket.emit
  }) 
});
  }).catch((err)=>console.log(err))
})

In the above code, I am looking for change in the room database (where online users are stored) and whenever there is a change I am looping through all the topics (100+) then search for online users count and emit them.

I know this is a bad piece of code. So I am looking for any help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Anyway, after a user connects to your server you save that client socket somewhere right? Mostly inside Redis or MemCached. To be more precise - the id of that socket. So, you can save additional data with it as well (topicId, userId). Then you can just count all sockets by filtering them with the topicId (as I understood you need active users per topic).

about 4 years ago · Juan Pablo Isaza Denunciar
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