Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to notify front-end from backend?

Tags:

javascript

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.

like image 671
konyv12 Avatar asked Jun 08 '17 07:06

konyv12


People also ask

How do you communicate front end and back-end?

Frontend and backend communicate with each other - via Http requests. The frontend will, for example, send entered data to the backend. The backend might then again validate that data (since frontend code can be tricked) and finally store it in some database.

How does front end work with backend?

Front-end developers work on what the user can see while back-end developers build the infrastructure that supports it. Front-end developers work on what the user can see while back-end developers build the infrastructure that supports it. Both are necessary components for a high-functioning application or website.

Can front end work without backend?

Front-end developer: Also called a front-end designer, they can create a site without any back-end development. The site they would create without a web developer, or using the backend, is a static site. A static site is something like a site for a restaurant or hair salon.

Where is front end code executed?

The front-end is the code that is executed on the client side. This code (typically HTML, CSS, and JavaScript) runs in the user's browser and creates the user interface.


2 Answers

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...)

like image 75
Jeremy Thille Avatar answered Oct 13 '22 06:10

Jeremy Thille


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.

like image 22
vinoth h Avatar answered Oct 13 '22 06:10

vinoth h