简体   繁体   English

如何在Rails中获得类似Facebook的通知系统

[英]How to get a Facebook like notification system in rails

Hey how to make a notification system like Facebook or diaspora in rails. 嘿,如何在Rails中制作一个像Facebook或Diaspora这样的通知系统。

I had tried making activity feed but that was not the thing I wanted I want an exactly same feature like this websites have. 我曾尝试制作活动供稿,但这不是我想要的,我想要一个与本网站完全相同的功能。

I have a simple app Where there are two types of users buyers and sellers 我有一个简单的应用,其中有两种类型的用户:买者和卖者

I want to notify the seller whenever a buyer comment on their products. 每当买家对商品发表评论时,我都希望通知卖家。

What you are looking at here is a server push implementation. 您在这里看到的是server push实现。 That means when some notification/action happens in the server, it should push a notification to your rails app. 这意味着当服务器中发生某些通知/操作时,它将通知发送到您的Rails应用程序。 The difference with @manju's answer is, its proposing aa solution based on your clients browser will call the server periodically for new notifications. @manju答案的不同之处在于,其基于您的客户端浏览器的aa提议解决方案将定期调用服务器以获取新通知。

There are two main ways to do this. 有两种主要方法可以做到这一点。

1 - Using some third party SASS solutions. 1-使用某些第三方SASS解决方案。 (easy way, but cost money ;)) (简单的方法,但是要花钱;)

Fire base , allows you to send push notifications to clients. Fire base ,使您可以将推送通知发送给客户端。

pusher is another provider offers the same kind of functionalists. pusher是另一个提供相同功能的提供程序的提供商。

Read their documentations, normally each of them have gems you can easily integrate to your rails app. 阅读他们的文档,通常每个人都有宝石,您可以轻松地将它们集成到Rails应用程序中。

2 - Implement your own push server 2-实施自己的推送服务器

You can implement your own push server with rails, and integrate to your app. 您可以使用Rails来实现自己的推送服务器,并集成到您的应用程序中。

Faye is a one option, Faye是一种选择,

But more exiting thing is Rails5 will have Action Cable which tries to solve the same issue. 但是更多的事情是Rails5将具有Action Cable来尝试解决相同的问题。 action cable gem 动作电缆宝石

and there are articles showing action cable with rails4 apps (you dont have to wait till rails5 comes out) , but I haven't used it personally yet. 并且有一些文章显示了带有rails4应用程序的动作电缆(您不必等到rails5出来),但是我还没有亲自使用它。

HTH 高温超导

Facebook does it using comet techniques. Facebook使用彗星技术来做到这一点

Here are some of the helpful links 以下是一些有用的链接

Link1 链接1

Link2 链接2

Link3 链接3

Here is the theory how facebook does 这是Facebook如何做的理论

  • Facebook works by polling the server for any changes. Facebook通过轮询服务器的任何更改来工作。

  • Facebook page will make ajax request to server and the ajax request will have much time out Facebook页面将向服务器发出ajax请求,而ajax请求将超时

  • But in the server-side in the API in server it will constantly poll DB server if anything has changed by constantly checking the activity log table in database ..if a change has been found it will return the result till then it will poll the DB 但是在服务器端API的服务器端中,它将通过不断检查数据库中的活动日志表来不断轮询DB服务器是否发生了任何更改。如果发现更改,它将一直返回结果,直到轮询数据库为止

  • Once Ajax request is complete it will recursively try again. Ajax请求完成后,将递归重试。

Here is a code snippet - Client side 这是一个代码段-客户端

function doPoll() {
   $.get("events.php", {}, function(result) {
      $.each(result.events, function(event) { //iterate over the events
          //do something with your event
      });
      doPoll(); 
      //this effectively causes the poll to run again as
      //soon as the response comes back
   }, 'json'); 
}

$(document).ready(function() {
    $.ajaxSetup({
       timeout: 1000*60//set a global AJAX timeout of a minute
    });
    doPoll(); // do the first poll
});

Here is a code-snippet in server side: 这是服务器端的代码片段:

while(!has_event_happened()) {
   sleep(5);
}

echo json_encode(get_events());

you can find it in much detail here 您可以在这里找到更多详细信息

you can actually adopt this approach according to your needs 您实际上可以根据需要采用这种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM