如何像 Facebook 那样构建实时推送通知功能? [英] How to build realtime push notification feature like facebook does?

查看:22
本文介绍了如何像 Facebook 那样构建实时推送通知功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为我的网络应用程序(一个小型​​社交网络)构建实时推送通知功能,但我不知道从哪里开始.

I'm going to build realtime push notification feature for my web application ( a small social network) and I don't know where to start.

这就是我想要构建的:有喜欢按钮,评论表单,......用户点击喜欢,写下他们的评论,并且(相对)立即,在所有者的浏览器上显示新的喜欢和评论的数量,... 类似的东西.

This is what I want to build: there are like buttons, comment forms, ... Users click like, write their comments and (relatively) immediately, on the owner's browser shows the number of new likes and comments, ... Something like that.

我已经在 nodeJs、MeteorJS 上阅读了有关 socketIo 的内容,但不幸的是,它们需要现代浏览器支持的 WebSocket.我刚刚阅读了 Comet technic,发现它很容易应用.但我不确定它是否会表现良好,因为 Comet 依赖于长轮询连接(如果我错了,请纠正我).

I've read about socketIo on nodeJs, MeteorJS but unfortunately, they need WebSocket supported by mordern browsers. I've just read about Comet technic and find it pretty easy to apply. But i'm not sure it will performs well because Comet relies on long-polling connection (correct me if I'm wrong).

此外,我认为 facebook 正在使用 Comet 作为其推送通知功能.通过萤火虫插件上的控制台选项卡,我可以看到始终与 facebook 保持连接.那么有人可以向我展示一种技术,一种开发这种功能的模型吗?

In addition, I think facebook is using Comet for its push notification feature. Through console tab on firebug plugin I can see there's alway a holding connection to facebook. So can anybody show me a technic, a model to develop a feature like that?

推荐答案

一个有前途的想法是使用 HTML5 通知 API;如果您希望在浏览器运行时在用户屏幕上弹出通知(即使您正在浏览另一个网站或关闭所有窗口),这将是完美的选择.

A promising idea is to work with the HTML5 notification API; it's perfect if you want notifications to pop on the user screen as long as his browser is running (even if you're surfing another website or if all windows are closed).

http://www.paulund.co.uk/html5-notifications

但是,如果您想要异步更新页面的不同部分(无需刷新或按下按钮),则应该一起使用:

However, if what you want is to update different parts of your page asynchronously (without refreshing or pushing a button), you should use together :

  • Ajax 调用;
  • 倾听者和观察者.

当您的 Ajax 调用检索特定类型的 json 数据(例如)时,它可以触发带有许多新通知的徽章(侦听器)的出现,等等......

When you Ajax calls retrieve particular types of json data (for example), it can trigger appearance of a badge (listener) with a number of new notifications, or so...

安装 JQuery 后,您应该没问题...

With JQuery installed, you should be fine...

尽管通常情况并非如此,但有时,对于简单的调整,对完成的工作进行编码会更容易...

Even though it's often not the case, sometimes, for simple tweaks, it's easier to code the job done...

你可以从这里开始:

如何实现观察者"?在没有插件的 Jquery 中?(它很旧,但很有趣)

或查看此页面:

使用 ajax/jquery 进行浏览器即时更新

(令人难以置信的谷歌查询返回 stacko 页面的频率)

(incredible how often google queries return stacko' pages)

这篇关于如何像 Facebook 那样构建实时推送通知功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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