简体   繁体   English

PHP + MySQL + JavaScript中的网络聊天

[英]Web Chat in PHP + MySQL +JavaScript

I am looking to create a Web Chat system using PHP, MySQL and JavaScript. 我正在寻找使用PHP,MySQL和JavaScript创建Web聊天系统的方法。

Currently, I am storing messages in a MySQL database with an incremental ID (Yes, it is indexed), a timestamp, the sender, and the message itself. 当前,我将消息存储在具有增量ID(是的,已建立索引),时间戳,发件人和消息本身的MySQL数据库中。 I am then using AJAX to query the database every 500ms, seeing if there are any more recent messages than the last one received. 然后,我使用AJAX每500毫秒查询一次数据库,查看是否有比收到的最后一条消息多的最新消息。 However, I have a feeling that this is probably horribly inefficient as it will result in a large load on the MySQL server when multiple users are online. 但是,我觉得这可能效率很低,因为当多个用户在线时,这会导致MySQL服务器上的负载很大。 Having looked around a bit on Google and on here, everything seems to point to this way of doing it. 在Google和此处浏览了一下之后,似乎一切都指向这种方式。

My question: is there a better way to do this? 我的问题是:有更好的方法吗? Any tips on how to reduce the load on the server would also be welcome. 也欢迎任何有关如何减少服务器负载的提示。

I'm using PHP 5.3, on an Apache webserver, so libraries or plugins compatible with those would be fine. 我在Apache网络服务器上使用的是PHP 5.3,因此与之兼容的库或插件会很好。

EDIT : Forgot to mention in the original post, but I'm not worried about supporting IE or other outdated browsers. 编辑 :忘记在原始帖子中提及,但是我不担心支持IE或其他过时的浏览器。

Potentially viable basic approach: 可能可行的基本方法:

  • Cache your 50 most recent messages in memcache. 将您的50条最新消息缓存在memcache中。 Reset this whenever a new entry is added to the database. 每当将新条目添加到数据库时,请重置此设置。 When new users connect, serve them these 50 messages to populate their chatroom. 当新用户连接时,向他们提供这50条消息以填充其聊天室。
  • Use a third party service like http://www.pubnub.com/ to send messages to your clients. 使用第三方服务(例如http://www.pubnub.com/)将消息发送给您的客户。 Whenever a new message is sent to your chatroom, send it out on pubnub. 每当有新消息发送到您的聊天室时,请在pubnub上将其发送出去。 Your server code will do this after writing to your database successfully. 成功写入数据库后,服务器代码将执行此操作。

notes: I'm not affiliated with pubnub. 注意:我不隶属于pubnub。 You don't need to use 50 messages above either. 您也不需要使用上面的50条消息。 You don't even have to give them any messages when they connect depending on how you want to set it up. 当他们连接时,您甚至不必向他们发送任何消息,这取决于您要如何设置。 The point is that you want to avoid your users reading from your database in this case - that model isn't likely to scale for this type of application. 关键是在这种情况下,您要避免用户从数据库中读取数据-该模型不太可能针对此类应用程序扩展。

Ideally, an evented environment would be ideal for this kind of app. 理想情况下,事件环境对于此类应用程序将是理想的。 The LAMP stack is not particularly well suited. LAMP堆栈不是特别适合。

You are asking about a web chat system specifically built in PHP, MySQL and HTML with JavaScript. 您正在询问专门用PHP,MySQL和HTML与JavaScript构建的Web聊天系统。 There are many options including Pre-built solutions: http://www.cometchat.com/ and http://www.arrowchat.com/ which all have chat comet services powered by a cloud offering like http://www.pubnub.com/ with options to host it yourself. 有很多选项,包括预建解决方案: http : //www.cometchat.com/http://www.arrowchat.com/ ,它们都具有由像http://www.pubnub这样的云产品提供支持的聊天彗星服务。 .com /,并带有可自行托管的选项。 See more about CometServices http://www.cometchat.com/cometservice/third-party-alternatives where you compare the service providers. 有关比较CometServices的更多信息,请访问http://www.cometchat.com/cometservice/third-party-alternatives ,在此您可以比较服务提供商。 There are several more options, however I recommend starting there. 还有其他几种选择,但是我建议从那里开始。 If you needs something more simple, like HTML and JavaScript only solution, you can check out http://www.pubnub.com/blog/build-real-time-web-apps-easy which is a blog about building real-time web apps easy with an example chat app in 10 lines of JavaScript Code. 如果您需要更简单的内容(例如仅HTML和JavaScript的解决方案),则可以查看http://www.pubnub.com/blog/build-real-time-web-apps-easy ,这是一个有关构建实时的博客10行JavaScript代码中的示例聊天应用程序可轻松实现Web应用程序。 The solution Cuts Development Time by providing full Cross Platform for all browsers and mobile devices. 该解决方案通过为所有浏览器和移动设备提供完整的跨平台来缩短开发时间。

I would recommend using this library, Pubnub . 我建议使用Pubnub这个库。 Pubnub is an easy way to send radio signals via javascript, or any TCP language (such as PHP) - and javascript instantly recieves the sent messages. Pubnub是一种通过javascript或任何TCP语言(例如PHP)发送无线电信号的简便方法-javascript会立即接收已发送的消息。

In PHP, you could simply have it save to your database - then use Pubnub's PHP API's to send the message to everyone else on the page. 在PHP中,您可以将其保存到数据库中,然后使用Pubnub的PHP API将消息发送给页面上的其他所有人。

If your familiar with Html, Javascript, and PHP - it can be fairly easy to learn. 如果您熟悉Html,Javascript和PHP,那么它很容易学习。 I would recommend it. 我会推荐。

You should look into ajax long polling, in a nutshell this a simple ajax call but will not return a result from the server if there is no new data. 简而言之,您应该研究ajax长轮询,这是一个简单的ajax调用,但是如果没有新数据,则不会从服务器返回结果。 You just do a simple loop on the server side until new data will be available then return it. 您只需在服务器端进行简单循环,直到有新数据可用,然后将其返回即可。 Of course you have to stop this eventually if there's no result to send to client after a while (eg. 1 minute) then restart the call. 当然,如果一段时间(例如1分钟)后没有结果发送给客户端,则最终必须停止此操作,然后重新启动呼叫。

Sockets are a better solution than AJAX polling, however isn't much around about how you can integrate socket based chats with MySQL. 与AJAX轮询相比,套接字是一种更好的解决方案,但是关于如何将基于套接字的聊天与MySQL集成在一起的问题并不多。

I have done a few tests and have a basic example working here: https://github.com/andrefigueira/PHP-MySQL-Sockets-Chat 我已经做了一些测试,并有一个基本的示例在这里工作: https : //github.com/andrefigueira/PHP-MySQL-Sockets-Chat

It makes use of Ratchet ( http://socketo.me/ ) for the creation of the chat server in PHP. 它利用Ratchet( http://socketo.me/ )在PHP中创建聊天服务器。

And you can send chat messages to the DB by sending the server JSON with the information of who is chatting, (if of course you have user sessions) 然后,您可以通过发送服务器JSON以及正在聊天的人的信息来向数据库发送聊天消息(如果您当然有用户会话)

I suppose, that chat is too intensive for storage engines MySQL. 我想,对于存储引擎MySQL来说,聊天太密集了。 Maybe, MEMORY table type will be ok, never used it. 也许, MEMORY表类型可以,但从未使用过。 I spoken to several developers and everybody agree, that best option for chat is Memcache or even writing your own custom daemon (with memory-only storage as weel). 我曾与数位开发人员交谈,每个人都同意,聊天的最佳选择是Memcache ,甚至编写自己的自定义守护程序(只有内存的存储方式)。

For client part you may read about short-polling, long-poling and web-sockets / sockets via flash/Java object. 对于客户端,您可以阅读有关通过Flash / Java对象进行的短轮询,长轮询和Web套接字/套接字的信息。

using AJAX to query the database every 500ms 每500毫秒使用AJAX查询数据库

Is short-polling. 是短轮询。

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

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