在Rails中实现通知 [英] Implementing Notifications in Rails

查看:72
本文介绍了在Rails中实现通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当用户在评论或帖子中提及他/她时,我想通知他.
用户句柄是@user_name,类似于 Facebook .
提及的数据库表如下:

In my application, I want to notify a user, when he/she is mentioned in a comment or a post.
The user handle is @user_name, similar to Facebook.
The database table for mentions looks like:

Mention
  mentioned_by: user_id (foreign key)
  user_mentioned: user_id (foreign key)
  comment_id: (foreign key)
  post_id: (foreign key)

我无法找到实现它的方法. Facebook/Twitter如何做到这一点?

I can't figure out a way to implement it though. How do Facebook / Twitter do it?

我决定使用的是ActiveRecord callbacks/ Observer design pattern,每当将新评论/帖子保存到数据库时,我都可以浏览帖子/评论的内容,查找是否有提及,然后执行通知根据需要.

What I decided to go with, was use ActiveRecord callbacks/ Observer design pattern and whenever a new comment/post is saved to database, I can go through the contents of the post/comment, and look out for any mentions and then execute the notifications as required.

我感觉到有些遗漏的东西,但我做得不好.
这是最好的方法吗?

I get the feeling that there are some missing pieces and I am not getting it right.
Is this the best way of doing it?

推荐答案

Facebook和Twitter不是中型Rails应用程序.他们是公司.运行它们的技术是分布式的,并且大多是自定义的,特别是在Facebook的情况下.

Facebook and Twitter are not mid-size Rails applications. They are companies. The tech that runs them is distributed and mostly custom, especially in the case of Facebook.

您似乎要掌握的部分是他们如何确定谁要通知谁.这就是狗屎成真的地方.您可以在它们背后找到很多有关体系结构的信息,当然,有很多很棒的东西可以帮助您考虑这些事情,但是最终它们都不会成为您在任何应用程序中实现的东西重新建设.

The part that you seem to be grasping for is how they determine who to notify in a performant and scalable way. This is where shit gets real. You can find a lot of information about the architecture behind each on of them, and there is certainly a lot of great stuff to help you think about these things, but ultimately none of it is going to be something you implement into whatever application you're building.

http://www.quora.com/What-is-Facebooks-architecture

Facebook体系结构

http://www.infoq.com/news/2009/06 /Twitter建筑

http://engineering.twitter.com/2010 /10/twitters-new-search-architecture.html

Quora上提供了更多多汁的细节.

Plenty more juicy details over at Quora.

不幸的是,这一切都无法使您更接近目标.我认为您最想做的事情就是简单地将 Pusher 之类的服务与向客户发送邮件而无需担心,请使用 ActiveRecord观察者将通知添加到后台队列,工人实际上将这些通知发送给Pusher.这是一天或更少的工作,并且应该可以很好地扩展到每天至少1万条通知.一旦开始出现性能问题,就放弃Observer和Pusher,使用像Goliath这样可以在本地处理这两项任务的工具.

Unfortunately, none of this gets you closer to your goal. I think the most realistic thing for you to do to start out with woud be to simply tie in a service like Pusher to send messages to clients without worrying about it, use an ActiveRecord Observer to add notifications to a background queue where the workers actually send those notifications to Pusher. This is a day or less of work and it should scale well to at least 10k notifications a day. Once you start having performance problems, ditch the Observer and Pusher for something like Goliath that can handle both of the jobs locally.

最重要的是,学习逐步掌握的大型,经验丰富的系统所能提供的知识,但主要只是看它们遇到了什么问题以及他们如何解决这些问题 em>.这些方法甚至在大公司中也不尽相同,并且每种实现方法都将有所不同.

Bottom line, learn what you can about large and experienced systems that have been put through the paces, but mainly just to see what problems they ran into and how they solved them. These methods aren't the same among the big guys even, and they are going to vary for each implementation.

希望这能为您指明一个正确的方向. :)

Hopefully that helps point you in a good direction. :)

这篇关于在Rails中实现通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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