如何推送通知与angular.js? [英] How to push notifications with angular.js?

查看:325
本文介绍了如何推送通知与angular.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在建立一个简单的应用程序来学习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 he project create "cards" with "todos" which can then be moved them from state to state ("backlog", "in progress", "complete", etc.)

我希望能够推送通知所有谁连告诉需要刷新获取最新的待办事项他们的应用程序的人。换句话说,假设用户A增加了一个新的卡项目A,我希望将消息发送到谁正在看项目A的所有用户,让他们的应用程序发出的一个项目刷新,以获得最新和最伟大的。

I would like to be able to push 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.

这是如何进行的任何建议吗?哪种技术,如果有的话,我需要添加到MEAN堆栈能够做这样的事?

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?

在此先感谢

推荐答案

由于你的平均堆栈上,在节点标准的建议是使用的 Socket.IO API。

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

客户端

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

服务器

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);
  });
});

它会使用WebSockets在可能的情况,并退回到AJAX长轮询或Flash轮询在浏览器中在没有WebSocket的支持。

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

至于有角整合,这里的一对 Socket.IO和角:

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

我会写关于如何整合Socket.IO添加实时
  功能到AngularJS应用。在本教程中,我将
  通过写一个即时消息应用程序走。

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.

这篇关于如何推送通知与angular.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆