如何使用Atmosphere设计推送通知 [英] How to design push notifications using Atmosphere

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

问题描述

我想用气氛来开发一个通知系统。

I want to use atmosphere to develop a notification System.

我对Atmosphere很新,所以如果我在某处错了,请道歉。
我理解的是当Actor发布我将通知操作保存到数据库的内容时。
我不明白接收者将如何实时接收这些通知。

I am very new to Atmosphere so apologies if I am wrong somewhere. What i understood is when a Actor publishes something I save the notification action to the database. What i don't understand how the receiver will receive those notifications in realtime.

我认识的发件人将执行以下操作

The sender i know will do something like following

event.getBroadcaster().broadcast(
            objectMapper.writeValueAsString("Some Message"));

现在我无法弄清楚接收者如何收到此消息。

Now i am not able to figure out how the receiver can receive this message.

例如。我想添加一个用户对象作为朋友。因此,当User1添加User2 User1广播时,而不是我如何将通知推送到User2。我很难理解这一点。

For example . I want to add a User Object as Friend. So when User1 adds User2 User1 broadcast but than how i push the notification to User2. I have difficulty in understanding this.

从技术上讲,我想要一些类似facebook或gmail通知的用户活动,其他用户会收到通知。

Technically i want something similar like facebook or gmail notification where on user activity other users get notifications.

推荐答案

基本上你需要的是实现发布 - 订阅

Basically what you need is to implement Publish-subscribe on top of Atmosphere.

气氛由两部分组成:客户端(基于javascript)和服务器端(基于java)。

Atmosphere consists of two parts: client-side (javascript-based) and server-side(java-based).

首先,您需要配置服务器端:安装大气层

First of all you need to configure server-side: Installing Atmosphere

即servlet或过滤器,它是必需的,以便它可以添加 AtmosphereResource HttpServletRequest

Namely servlet or filter, it is required so that it could add AtmosphereResource to the HttpServletRequest.

AtmosphereResource 代表服务器端的单个客户端连接。

AtmosphereResource represents a single client connection on the server-side.

Broadcaster 实际上是这些资源的容器,因此您无需处理需要发送到多个连接时查找/迭代/并发。 (请注意,单个客户端可以生成多个连接)。

Broadcaster is actually a container for these resources, so that you don't need to handle lookup/iteration/concurrency when you need to send to multiple connections. (Note that multiple connections can be produced by single client).

在服务器端,您需要为客户端提供一个端点来订阅通知。
例如,如果您使用的是Spring-MVC,它可以像这样(省略验证/认证等):

On the server-side you need to provide clients an endpoint to subscribe for notifications. For example, if you are using Spring-MVC, it could go like this (omitting validations/authentications, etc.):

@RequestMapping(value = "/user-notifications/{userId}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void watch(@PathVariable("userId") String userId,
                  HttpServletRequest request) throws Exception {
    //Atmosphere framework puts filter/servlet that adds ATMOSPHERE_RESOURCE to all requests
    AtmosphereResource resource = (AtmosphereResource)request.getAttribute(ApplicationConfig.ATMOSPHERE_RESOURCE);

    //suspending resource to keep connection
    resource.suspend();

    //find broadcaster, second parameter says to create broadcaster if it doesn't exist
    Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(userId,true);

    //saving resource for notifications
    broadcaster.addAtmosphereResource(resource);
}

当事情发生时你可以通知这样的客户:

When something happens you can notify clients like this:

public void notify(User user, Event event){
    Broadcaster b = BroadcasterFactory.getDefault().lookup(user.getId());
    if (b!=null){
        b.broadcast(event);
    }
}

在客户端,您需要发送订阅请求并监听后续事件,如下所示:

On the client side you need to send a subscribe request and listen for subsequent events, like this:

var request = new atmosphere.AtmosphereRequest();
request.url = '/user-notifications/'+userId;
request.transport = 'websocket';
request.fallbackTransport = 'streaming';
request.contentType = 'application/json';
request.reconnectInterval = 60000;
request.maxReconnectOnClose = 1000;
request.onMessage = function(response){
    console.log(response);
    alert('something happend<br>'+response);
};
that.watcherSocket = atmosphere.subscribe(request);

所以,总结一下:


  1. 客户端发送请求我想收到此类通知。

  2. 服务器接收请求,暂停并保存连接(在您的代码或Broadcaster中) )。

  3. 当发生某些事情时,服务器会查找挂起的连接,并在其中发送通知。

  4. 客户端收到通知并调用回调。

  5. 利润!!!

  1. Client sends request "I want to receive this kind of notifications".
  2. Server receives request, suspends and saves connection somewhere (either in your code or in Broadcaster).
  3. When something happens server looks for suspended connection and sends notification in it.
  4. Client receives notification and callback is invoked.
  5. Profit!!!

这个wiki 解释了Atmosphere背后的一些概念以及其他文档的链接。

This wiki has explanations for some concepts behind Atmosphere and links to other documentation.

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

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