简体   繁体   English

Socket.io用于实时应用

[英]Socket.io for real time application

So I have another project that I built a few months ago. 所以我有几个月前建立的另一个项目。 It currently uses jquery with ajax and makes a call to a php script every 30 seconds. 它目前使用jquery和ajax并每隔30秒调用一次php脚本。 The php script queries mysql and passes back the results to display. php脚本查询mysql并传回结果以显示。 In this application the person has the ability to add/edit/remove records from the db and when they do the list of results from the db needs to update the list they see. 在这个应用程序中,该人员能够从数据库中添加/编辑/删除记录,当他们这样做时,数据库的结果列表需要更新他们看到的列表。 So far it seems to do the job, but looking down the road the db will get bigger and it will need to be more in real time, meaning not just refresh every 30 seconds but as instantaneous as possible. 到目前为止它似乎完成了这项工作,但是沿着这条道路看,数据库将变得越来越大,它需要更加实时,这意味着不仅每30秒刷新一次,而且尽可能瞬间刷新。

Would socket.io be the answer to this? socket.io会解决这个问题吗? Would I simply use socket.io with nodejs to create a server and emit events to query my db and return the results to display? 我是否只需使用socket.io和nodejs来创建服务器并发出事件来查询我的数据库并返回结果显示? Then on the client side just have a function that calls the server socket script like every second? 然后在客户端只有一个函数,像每一秒调用服务器套接字脚本? Would that be the correct path to using socket.io? 这是使用socket.io的正确途径吗? If so would there be a concern with this process if I ran it every second with regards to server resources? 如果我在服务器资源方面每秒运行一次,那么这个过程是否会引起关注?

Would socket.io be the answer to this? socket.io会解决这个问题吗?

My answer is that socket.io / node.js is much better at dealing with real-time applications then PHP in the current form. 我的答案是socket.io / node.js在处理实时应用程序方面要比当前形式的PHP好得多。

Then on the client side just have a function that calls the server socket script like every second? 然后在客户端只有一个函数,像每一秒调用服务器套接字脚本?

In node.js you should not poll ask for the information, but instead you should have the information pushed(use redis pubsub or node.js events for that) to you. 在node.js中,您不应该轮询请求信息,而是应该推送信息(使用redis pubsub或node.js事件)。 I advice you to have a look at a pubsub snippet from me on stackoverflow.com to explain myself a little bit. 我建议你在stackoverflow.com上看看我的pubsub片段来解释一下自己。 For the code to work you will need to install socket.io 0.6.x(0.6.18) because the latest socket.io 0.7.x has a slightly different API. 要使代码生效,您需要安装socket.io 0.6.x(0.6.18),因为最新的socket.io 0.7.x有一个稍微不同的API。

If so would there be a concern with this process if I ran it every second with regards to server resources? 如果我在服务器资源方面每秒运行一次,那么这个过程是否会引起关注?

Like I said before use pubsub semantics. 就像我之前说的那样使用pubsub语义。 You could for example use: 你可以使用例如:

Would socket.io be the answer to this? socket.io会解决这个问题吗? Would I simply use socket.io with nodejs to create a server and emit events to query my db and return the results to display? 我是否只需使用socket.io和nodejs来创建服务器并发出事件来查询我的数据库并返回结果显示? Then on the client side just have a function that calls the server socket script like every second? 然后在客户端只有一个函数,像每一秒调用服务器套接字脚本? Would that be the correct path to using socket.io? 这是使用socket.io的正确途径吗? If so would there be a concern with this process if I ran it every second with regards to server resources? 如果我在服务器资源方面每秒运行一次,那么这个过程是否会引起关注?

If you want something realtime. 如果你想要实时的东西。 The structure might be different. 结构可能不同。

First you need a middle man which help you communicate between browser and server. 首先,您需要一个帮助您在浏览器和服务器之间进行通信的中间人。

Solution 1 : Request every 30 seconds to get realtime (Same as your current method) 解决方案1:每30秒请求一次实时(与当前方法相同)

Solution 2 : http Streaming. 解决方案2:http Streaming。 Once request has make to the server. 一旦请求到服务器。 Server can continue send response to browser. 服务器可以继续向浏览器发送响应。 Due to many security browser issues, socket.io has born. 由于许多安全浏览器问题,socket.io已经诞生。 Socket.io provide many method streaming, htmlfile stream,xhr request,flash... Socket.io提供了很多方法流,htmlfile流,xhr请求,flash ...

Second, you need server to accept the connection and make a long poll services. 其次,您需要服务器接受连接并进行长轮询服务。 Thank god ! 感谢上帝 ! socket.io has done this part for you. socket.io已经为你做了这部分。

Thirdly, the most important person ! 第三,最重要的人! That is data. 那就是数据。

Solution 1 : Every request/trigger Call db or nosql database (SQLserver,mysql,Mongodb). 解决方案1:每个请求/触发器调用db或nosql数据库(SQLserver,mysql,Mongodb)。 Believe me ! 相信我 ! Your db going die soon. 你的数据库很快就会死掉

Solution 2: Messaging services. 解决方案2:消息传递服务。 Services like Redis pub/sub, rabbitQ. 像Redis pub / sub,rabbitQ这样的服务。 It simply use subscribe and publish message in queue. 它只是在队列中使用订阅和发布消息。 It use particular protocol to publish message and they did not store message like db. 它使用特定协议发布消息,并且它们不存储像db这样的消息。 So its can send message >100 k request /seconds ! 所以它可以发送> 100 k请求/秒的消息! Super fast.Wow ! 超级快。哇! Is it use this services can solved realtime solution ? 是否使用此服务可以解决实时解决方案? Unfortunatly that a bit hard. 不幸的是,有点难。 Why ? 为什么? Because use this kind of services you cannot store data, SQL query to customize your data need. 因为使用这种服务你无法存储数据,SQL查询需要自定义你的数据。

Solution 3 : In-memory process.Redis,memcached. 解决方案3:内存中的进程.Redis,memcached。 Memory is super fast! 记忆力超快! You can store your final display results in memory. 您可以将最终显示结果存储在内存中。 Memory can reached >100 k request/seconds. 内存可以达到> 100 k请求/秒。

Conclusion. 结论。 To build a really realtime web application like facebook or tweeter. 构建一个真正的实时Web应用程序,如Facebook或tweeter。 We need to apply all solution above + some cheat. 我们需要应用以上所有解决方案+一些作弊。 Example, facebook friend notification. 例如,facebook朋友通知。 When you get friend news update, it use messaging services to publish alert to all friends (Max 5000 people).Because heavy publish will slow down message performance. 当你获得朋友新闻更新时,它会使用消息传递服务向所有朋友发布警报(最多5000人)。因为大量发布会降低消息性能。 After that cache (Memcached) the notification message to reduce calling db(mysql). 之后缓存(Memcached)通知消息以减少调用db(mysql)。 How about we can get group message with over 1 million people ? 我们如何能够获得超过100万人的团体信息? Of course we dun publish to all over 1 million people. 当然,我们出版给100多万人。 But we ask 1 million people to call our latest group messages which store in memory. 但我们要求100万人拨打我们最新的群组信息,这些信息存储在内存中。

  • Reminder : Scaling your server is important to achieve the realtime goal. 提醒:扩展服务器对于实现实时目标非常重要。 Good luck ! 祝好运 !

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

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