I created a ajax chat application something like this to check & get messages every second. and it works fine.
我创建了一个像这样的ajax聊天应用程序,每秒检查和获取消息。它工作正常。
function get_messages(user_id) {
$.ajax({
type : "POST",
url : "messages/get_messages",
cache : false,
data : {
user_id : user_id
},
success : function(data) {
if(data != '') {
var obj = $.parseJSON(data);
var messages = obj.messages;
}
}
});
setTimeout(function() { get_messages(user_id) }, 1000);
}
My question is, When lot of people use this application a lot to Ajax requests to server every second, is there any performance issue or server issue in doing like this, . What is the best practice for doing this ??
我的问题是,当很多人每秒都对Ajax请求服务器使用这个应用程序时,这样做是否有任何性能问题或服务器问题,。这样做的最佳做法是什么?
Thank you for your valuable suggestions :)
谢谢你的宝贵建议:)
2 个解决方案
#1
2
The best way to do chats like this is having the "chat window" properly said as an <iframe>
with a permanent connection to a script that will remain running and feeding the client with the new messages so you don't have to overwhelm the server with AJAX requests. This can be achieved by calling a ob_flush()
(just to make sure) and flush()
after printing new stuff, causing the client to receive the updates immediately. But first you have to prepare the PHP to behave properly by doing some settings:
像这样进行聊天的最佳方式是将“聊天窗口”恰当地称为
ini_set('zlib.output_compression', 'off');
ini_set('output_buffering', 'off');
set_time_limit(0);
If you are going to use sessions, don't forget sessions are locked to prevent concurrent writes, so after gathering the information you need from $_SESSION
you must release the session by using session_write_close()
otherwise the user will be unable to post messages etc.
如果您要使用会话,请不要忘记会话被锁定以防止并发写入,因此在从$ _SESSION收集您需要的信息后,您必须使用session_write_close()释放会话,否则用户将无法发布消息等。
Your script should also check for inactivity and output something to the client if the chat window remain idle for more than a couple minutes. It prevents the connection from being terminated by the browser. It doesn't have to be anything visual, something commented like <!-- keep alive -->
will do.
如果聊天窗口闲置超过几分钟,您的脚本还应检查是否不活动并向客户端输出内容。它可以防止浏览器终止连接。它不必是任何视觉效果,评论如 会做。
Now, where you gonna get the new messages from? There are a couple options for doing that:
现在,你将从哪里获得新消息?这样做有几种选择:
-
Sockets. You can have this Chat Server application running in server-side that all the Chat Window PHP scripts will connect to to be fed with the new chat lines. When a user submit a new message, its sent to the Chat Server and it broadcast to the Chat Window scripts. This Chat Server can safely be written in PHP too!
套接字。您可以在服务器端运行此Chat Server应用程序,以便所有聊天窗口PHP脚本将连接到新的聊天行。当用户提交新消息时,将其发送到聊天服务器并将其广播到聊天窗口脚本。这个聊天服务器也可以安全地用PHP编写!
-
A file. The easiest way. Every Chat Window PHP script open this same file for read-only, and
fseek()
to its end. Loops checking if its!feof()
a couple times per second to read the new lines from it, if theres any. When a user send a new message you just have append this message to the file and the trick is done.一份文件。最简单的方法。每个聊天窗口PHP脚本打开同一个文件为只读,fseek()到最后。循环检查它是否!feof()每秒几次从它读取新行,如果有的话。当用户发送新消息时,您只需将此消息附加到文件中即可完成操作。
-
SQL. Not recommended because every Chat Window PHP script will open a new connection to the RDBMS and eventually will reach its limit, but you can try SQLite that don't use RDBMS.
SQL。不建议使用,因为每个Chat Window PHP脚本都会打开一个到RDBMS的新连接,并最终达到其极限,但您可以尝试不使用RDBMS的SQLite。
#2
1
Using regular Ajax/Php for this task is not preferable. As you stated that what if there is alot of users, each user will query the database every second.
使用常规Ajax / Php执行此任务并不可取。正如您所说,如果有很多用户,每个用户将每秒查询一次数据库。
This puts too much overload on your server and the users will not have realtime communication with each other.
这会使您的服务器过载,并且用户之间无法实时通信。
I would suggest you to use node.js for this task. To make it cross-browser compatible you need to use a framework of node.js which is socket.io
我建议你使用node.js来完成这项任务。要使其跨浏览器兼容,您需要使用node.js的框架,即socket.io
So the final verdict, use node.js
所以最后的判决,使用node.js
You can learn node.js http://www.nodebeginner.org/
你可以学习node.js http://www.nodebeginner.org/
There are very good tutorials in the web. lynda.com has also very good tutorial on node.js
网上有很好的教程。 lynda.com也有关于node.js的非常好的教程
#1
2
The best way to do chats like this is having the "chat window" properly said as an <iframe>
with a permanent connection to a script that will remain running and feeding the client with the new messages so you don't have to overwhelm the server with AJAX requests. This can be achieved by calling a ob_flush()
(just to make sure) and flush()
after printing new stuff, causing the client to receive the updates immediately. But first you have to prepare the PHP to behave properly by doing some settings:
像这样进行聊天的最佳方式是将“聊天窗口”恰当地称为
ini_set('zlib.output_compression', 'off');
ini_set('output_buffering', 'off');
set_time_limit(0);
If you are going to use sessions, don't forget sessions are locked to prevent concurrent writes, so after gathering the information you need from $_SESSION
you must release the session by using session_write_close()
otherwise the user will be unable to post messages etc.
如果您要使用会话,请不要忘记会话被锁定以防止并发写入,因此在从$ _SESSION收集您需要的信息后,您必须使用session_write_close()释放会话,否则用户将无法发布消息等。
Your script should also check for inactivity and output something to the client if the chat window remain idle for more than a couple minutes. It prevents the connection from being terminated by the browser. It doesn't have to be anything visual, something commented like <!-- keep alive -->
will do.
如果聊天窗口闲置超过几分钟,您的脚本还应检查是否不活动并向客户端输出内容。它可以防止浏览器终止连接。它不必是任何视觉效果,评论如 会做。
Now, where you gonna get the new messages from? There are a couple options for doing that:
现在,你将从哪里获得新消息?这样做有几种选择:
-
Sockets. You can have this Chat Server application running in server-side that all the Chat Window PHP scripts will connect to to be fed with the new chat lines. When a user submit a new message, its sent to the Chat Server and it broadcast to the Chat Window scripts. This Chat Server can safely be written in PHP too!
套接字。您可以在服务器端运行此Chat Server应用程序,以便所有聊天窗口PHP脚本将连接到新的聊天行。当用户提交新消息时,将其发送到聊天服务器并将其广播到聊天窗口脚本。这个聊天服务器也可以安全地用PHP编写!
-
A file. The easiest way. Every Chat Window PHP script open this same file for read-only, and
fseek()
to its end. Loops checking if its!feof()
a couple times per second to read the new lines from it, if theres any. When a user send a new message you just have append this message to the file and the trick is done.一份文件。最简单的方法。每个聊天窗口PHP脚本打开同一个文件为只读,fseek()到最后。循环检查它是否!feof()每秒几次从它读取新行,如果有的话。当用户发送新消息时,您只需将此消息附加到文件中即可完成操作。
-
SQL. Not recommended because every Chat Window PHP script will open a new connection to the RDBMS and eventually will reach its limit, but you can try SQLite that don't use RDBMS.
SQL。不建议使用,因为每个Chat Window PHP脚本都会打开一个到RDBMS的新连接,并最终达到其极限,但您可以尝试不使用RDBMS的SQLite。
#2
1
Using regular Ajax/Php for this task is not preferable. As you stated that what if there is alot of users, each user will query the database every second.
使用常规Ajax / Php执行此任务并不可取。正如您所说,如果有很多用户,每个用户将每秒查询一次数据库。
This puts too much overload on your server and the users will not have realtime communication with each other.
这会使您的服务器过载,并且用户之间无法实时通信。
I would suggest you to use node.js for this task. To make it cross-browser compatible you need to use a framework of node.js which is socket.io
我建议你使用node.js来完成这项任务。要使其跨浏览器兼容,您需要使用node.js的框架,即socket.io
So the final verdict, use node.js
所以最后的判决,使用node.js
You can learn node.js http://www.nodebeginner.org/
你可以学习node.js http://www.nodebeginner.org/
There are very good tutorials in the web. lynda.com has also very good tutorial on node.js
网上有很好的教程。 lynda.com也有关于node.js的非常好的教程