Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push notifications with angular.js?

I have been building a simple application to learn angular.js. So far I hooked up all the pieces in the MEAN stack and I am able to save and retrieve data from Mongo.

The app is essentially a todo list. The user can create a project and inside the project create "cards" with "todos" which can then be moved from state to state ("backlog", "in progress", "complete", etc.)

I would like to be able to push the notifications to all the people who are connected to tell their apps that a refresh is needed to get the latest todos. In other words, let's say that user A adds a new card to project A, I would like to send a message out to all users who are currently watching project A so that their application issues a project refresh to get the latest and greatest.

Any suggestions on how to proceed? Which technology, if any, I need to add to the MEAN stack to be able to do something like this?

Thanks in advance

like image 566
Francesco Gallarotti Avatar asked Dec 01 '13 22:12

Francesco Gallarotti


People also ask

How to send push notifications with angular service worker?

Once we have the Angular Service Worker installed, we can now request the user permission for sending Push Notifications: the user clicks on the Subscribe button and the subscribeToNotifications () method gets executed

How do push notifications work?

Your web browser's push notification API gives web applications the ability to receive messages from a server whether or not the web app is in the foreground or currently loaded on a user agent. This lets you deliver asynchronous notifications and updates to users who opt-in, resulting in better engagement with timely new content.

How to trigger notifications when the application is not running?

This notification can be triggered locally by any application which is running or they can push the notification information fetched from the server to the user device when the application is not running. This will engage the user to get the information on time and does not need to remember the information.

What is notifications in browser?

Notifications is a message pushed to user's device passively. Browser supports notifications and push API that allows to send message asynchronously to the user. Messages are sent with the help of service workers, it runs as background tasks to receive and relay the messages to the desktop if the application is not opened.


3 Answers

Since you're on the MEAN stack, the standard recommendation in Node would be to use the Socket.IO API.

They provide the following example of two way messaging (which would facilitate your push messages very easily):

Client

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

Server

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

It will use websockets where possible, and fallback to AJAX long polling or Flash polling in browsers where there is no websocket support.

As for integrating with Angular, here's a good blog post on Socket.IO and Angular:

I'll be writing about how to integrate Socket.IO to add real-time features to an AngularJS application. In this tutorial, I'm going to walk through writing a instant messaging app.

like image 142
Ryan Weir Avatar answered Oct 19 '22 01:10

Ryan Weir


If you're already working with Express, you should check out express.io.

It has a bunch of cool features like Session support and the ability to forward normal HTTP routes to realtime routes.

like image 25
McMeep Avatar answered Oct 18 '22 23:10

McMeep


Here is a module we have written for getting AngularJS push notifications working in PhoneGap / Cordava (with full instructions): http://www.scorchsoft.com/blog/free-angularjs-cordova-push-notification-plugin/

Simply download the example code and install. There is also code included for setting up the pushing component in PHP.

like image 4
Andrew Ward Avatar answered Oct 19 '22 00:10

Andrew Ward