Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push data to client using node

Tags:

node.js

I'm using node to make an email client. I want to update the browser window as soon as any new email arrives. I'm already fetching the emails on server side using mail-listener2. I want to push that data to client side. How do i do that?

like image 727
Vivek Avatar asked Jul 21 '14 06:07

Vivek


People also ask

How do I send data from server to client in node JS?

Methods to send response from server to client are:Using send() function. Using json() function.

How do I use Web push in node JS?

In order to set up web push, you need to create a set of VAPID keys. VAPID keys identify who is sending the push notification. The web-push npm module can generate VAPID keys for you, so let's install web-push along with some other dependencies and use web-push generate-vapid-keys to create the keys.

Can I use node on the client side?

Other reasons why we cannot use node modules at the client side is that the node uses the CommonJS module system while the browser uses standard ES Modules which has different syntax.


1 Answers

Look at using websockets to push data in realtime to the client (browser). I would recommend using http://socket.io. They have some really great demos on there to show how you would do something like this.

Your server would send socket.io messages called message, containing the message details as JSON. Your client would listen for message events and update the UI as they come in.

like image 183
Matt Harrison Avatar answered Oct 06 '22 08:10

Matt Harrison