Socket.io实时应用 [英] Socket.io for real time application

查看:225
本文介绍了Socket.io实时应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我建立了一个几个月前的另一个项目。它目前使用jQuery的阿贾克斯,使调用PHP脚本每30秒。在PHP脚本查询mysql和传回的结果显示。在本申请的人必须从分贝添加/编辑/删除记录的能力和当他们这样做从分贝结果列表需要更新该列表,他们看到。到目前为止,似乎做的工作,但看在路上的分贝会变得越来越大,这将需要更多的实时的,这意味着不只是刷新每隔30秒,但瞬时越好。

请问socket.io是这个问题的答案?我会简单地使用socket.io与nodejs创建一个服务器,并发出事件查询我的分贝,并返回结果显示?然后在客户端只需要一个函数,调用,例如每一秒的服务器套接字脚本?请问这是否正确的路径使用socket.io?如果是的话会有这个过程的关注,如果我跑它的每一秒的问候服务器资源?

解决方案
  

请问socket.io是这个问题的答案?

我的回答是,socket.io / Node.js的是在当前表单处理实时应用程序,然后PHP好多了。

  

然后在客户端只是有一个   函数来调用服务器套接字   像每一个第二个脚本?

在Node.js的,你不应该问轮询的信息,而是你应该有推信息(使用Redis的发布订阅或Node.js的事件的)给你。我建议你​​看看<一href="http://stackoverflow.com/questions/4441798/how-to-use-redis-publish-subscribe-with-nodejs-by-event-driven/4446424#4446424">a PubSub的片段从我上stackoverflow.com解释自己一点点。对于code正常工作,您需要安装socket.io 0.6.x(0.6.18),因为最新的socket.io 0.7.x有一个稍微不同的API。

  

如果这样会有一个关注   这个过程中,如果我跑它的每一秒   至于服务器资源?

就像我之前使用PubSub的语义说。例如,你可以使用:

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. The php script queries mysql and passes back the results to display. 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.

Would socket.io be the answer to this? 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? 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? 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?

My answer is that socket.io / node.js is much better at dealing with real-time applications then PHP in the current form.

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. I advice you to have a look at a pubsub snippet from me on stackoverflow.com to explain myself a little bit. 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.

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. You could for example use:

这篇关于Socket.io实时应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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