4

I am using multer for file uploading. On the server I have this:

stream.on('finish', () => {                                                                              
  console.log('finished')                                                                                
  next()
})

What would be the best way (with what command) to notify the frontend that 'finished' has happened? I want to know this in order to have a loading bar.

konyv12
  • 638
  • 1
  • 8
  • 21

3 Answers3

4

That's what websockets are for. The server can send data to the client, and vice-versa. Check out socket.io.

Server : socket.emit("upload finished")

Client : socket.on("upload finished", function...)

Jeremy Thille
  • 21,780
  • 7
  • 36
  • 54
1

While Jeremy Thille 's answer works well, beware of broadcasting event to all.

Socket.emit() simply broadcasts message to all connected clients.

So you need to store the client id of the uploader and notify the progress only to that client and not all. Refer this.

vinoth h
  • 471
  • 3
  • 11
0

You can use sockets for this type of communication. socket.io is a node package for web sockets. In the server you have

socket.emit('done')

whereas in the client side you can attach to the emit to receive new data

socket.on('done', function (data) {
    // Do something with data
}); 
Ivan Mladenov
  • 1,525
  • 7
  • 15