简体   繁体   English

Facebook直播功能?

[英]Facebook live feed functionalty?

I am trying to make something like facebook live feeds, for example: when someone likes something or comments on something, the page updates without refreshing it! 我正在尝试制作类似Facebook直播的内容,例如:当有人喜欢某些内容或对某些内容发表评论时,页面会更新而不会刷新! I want to know which is the proper way to do this? 我想知道哪种方法可以做到这一点? regards 问候

Realtime updates in a web application is a hard problem because a single server handling many simultaneous long-lived TCP connections is a hard problem . Web应用程序中的实时更新是一个难题,因为处理许多同时长期TCP连接的单个服务器是一个难题

This is essentially impossible on a traditional web server like Apache + PHP because it allocates an entire OS thread for each incoming connection. 这在Apache + PHP等传统Web服务器上基本上是不可能的,因为它为每个传入连接分配了一个完整的OS 线程 Threads have significant overhead (like ~2 MB of RAM just for the stack space , plus whatever heap memory your application needs), so as few as a few hundred clients having your page open at the same time can bring a small server to its knees, and even an extra-large (and extra-expensive) hundred-GB-of-RAM server can only handle a few thousand concurrent connections. 线程有很大的开销(比如大约2 MB的RAM只用于堆栈空间 ,加上你的应用程序需要的任何堆内存),所以只有几百个客户端同时打开你的页面就可以让小型服务器瘫痪甚至一个超大(而且特别昂贵)的百GB RAM服务器也只能处理几千个并发连接。

Realtime communications is where Node really shines. 实时通信是Node真正发挥作用的地方。 Its single-threaded, event-driven architecture can easily support 2,000 concurrent connections on a commodity laptop, because each incoming connection is a small (a few kilobytes) heap allocation. 它的单线程事件驱动架构可以轻松支持商用笔记本电脑上的2,000个并发连接,因为每个传入连接都是一个很小的(几千字节)堆分配。 The limiting factor actually becomes the CPU and the underlying OS's TCP stack. 限制因素实际上变成了CPU和底层操作系统的TCP堆栈。

My recommendation is to take a look at Node – this is exactly the kind of problem it is designed for. 我的建议是看一下Node - 这正是它的设计问题。 You already know JavaScript, so it's really just a matter of the API and mastering Node's async, event-driven nature. 您已经了解了JavaScript,因此它只是API的一个问题,并且掌握了Node的异步,事件驱动特性。

You'll probably want to use Express for your HTTP server needs and use Socket.io for the realtime communications. 您可能希望使用Express来满足您的HTTP服务器需求,并使用Socket.io进行实时通信。

Socket.io is especially wonderful because its client-side library abstracts away all of the drudgery of cross-browser support: Socket.io特别棒,因为它的客户端库抽象了所有跨浏览器支持的苦差事:

  • In A-grade browsers , it connects to your server via WebSockets . A级浏览器中 ,它通过WebSockets连接到您的服务器。 This gets you a TCP socket that remains connected indefinitely, over which you can push arbitrary data at any time. 这将使您获得一个无限期保持连接的TCP套接字,您可以随时推送任意数据。
  • In downlevel browsers, it uses a fallback mechanism: 在下层浏览器中,它使用回退机制:
    • A Flash-based transport like WebSockets, but requires Flash player (if available) 基于Flash的传输,如WebSockets,但需要Flash播放器(如果可用)
    • AJAX long polling AJAX长轮询
    • And some more esoteric fallbacks if neither of those work 如果这些都不起作用,还有一些更深奥的后备

You can use long polling, yes. 你可以使用长轮询,是的。 Or, you can start to innovate and start using HTML5's connectivity capabilities and REALTIME the sh*t out of your site. 或者,您可以开始创新并开始使用HTML5的连接功能,并实现您网站的实时性。 There are already several out-of-the-box solutions for that, my favourite being the xRTML Realtime Framework . 已经有几个开箱即用的解决方案,我最喜欢的是xRTML Realtime Framework

Check it out 看看这个

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

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