1

I am implementing an HTTP Long Polling for an Express app but i encounter an error. Here is how the app should behave:

The app is a Evernote like, users can POST new note to the app [Note(txt, author, date)]. Users can subscribe (GET /subscribe/:author) to an author, so when a author post a new note, he should receive the response of GET /subscribe/:author.

I achieved to do this but with any note (not the good author) but I have an issue when I put a comparaison to see if the note is posted by the good author. Here is my code:

subscribe.js

router.get('/:auteur', (req, res, next) => {

  const auteur = req.params.auteur;

  rc.lrange("notes", 0, -1, (err, reply) => {
    const notes = reply.map( note => { return JSON.parse(note) })
    const notesAuteur = notes.filter(note => note.auteur == auteur)

    subscriber.on("message", (channel, message) => {
      const noteRecu = JSON.parse(message)
      // if(noteReceived.author == authorAsked)
      if(noteRecu.auteur == auteur) {
        // Add this new note to old one
        notesAuteur.push(noteRecu)
        // Responde
        res.send(notesAuteur)
      }
    })
  })
})

subscriber.subscribe("notes_publish")

Here is a bugged scenario:

  • user subscribe to "john" ( GET /subscribe/john )
  • "alex" post a new note ( POST /notes ) #Server don't respond
  • "john" post a new note ( POST /notes ) #Server error : "Can't set headers after they are sent."

How to tell my server :

if(note.author == goodAuthor) {
    res.send(data)
} else {
    Don't()
}

Thanks for your help !

Maxime Chevallier
  • 277
  • 1
  • 4
  • 14
  • regarding your server error, you might want to take a look at this SO post: https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client – Amiga500 Mar 20 '18 at 15:34

0 Answers0