开发者

is ajax-PHP-MySQL a good combination for a chat room app?

开发者 https://www.devze.com 2023-04-07 02:16 出处:网络
I plan to create a small chat room for my friends at my University. As I don\'t want to invest any money, I will use a free host which doesn\'t allow me to install an IRC server.Also I like using ajax

I plan to create a small chat room for my friends at my University. As I don't want to invest any money, I will use a free host which doesn't allow me to install an IRC server.Also I like using ajax and PHP as I already know them.

Is a self-refreshing ajax and PHP page a good idea? like each second, the ajax triggers a PHP script which returns latest, let's say 20 MySQL entries in the chat history.

When a user writes something, it is inserted in the MySQL DB, as you probably already figured out.

Is this a good idea?

Do you have any o开发者_如何学JAVAther idea for saving the messages? something more optimized than MySQL?

Or any totally different idea that could fulfill my purpose?

Thanks in advance!

andy :)

EDIT: what is better: MySQL DB or text file? (text file is preferred by jquery, why?)


You can save a lot of HDD speed if you using memory tables, and you nee choose right indexes.

Example DB structure:

CREATE TABLE `chat` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `channel` varchar(16) NOT NULL,
 `user` varchar(32) NOT NULL,
 `text` varchar(255) NOT NULL,
 `private` varchar(32) DEFAULT NULL,
     PRIMARY KEY (`id`),
 KEY `private` (`private`),
 KEY `channel` (`channel`)
) ENGINE=MEMORY

I got about 70-80 users online, but don't get any load on server (server with 2GHz CPU)


Maybe a free host should have bad database. So, if you continously access to it, your chat would be really slow (unless the chat is not going to be use a lot).

Anyways, there are some free options that you can use for your purposes (instead of developing by yourself):

http://www.phpfreechat.net/

http://www.phpopenchat.org/

http://hot-things.net/blab-lite-ajax-chat

And here are some tutorials with examples:

http://css-tricks.com/4371-jquery-php-chat/

http://www.tutorialized.com/tutorials/PHP/Chat-Systems/1

http://php.resourceindex.com/Complete_Scripts/Chat/Shoutbox/

Regards!


This is a pretty typical approach to a basic chat room and seems just fine to me. The only thing I would suggest is assign each chat record an auto-increment id so your ajax can track the latest message it's received. This way, if the last received message has an ID of 100, ajax can request items that have an ID of 101 or higher. Then you're only pulling new messages and they can just be appended to the chat window, instead of refreshing the whole page.


Why would you want to invent electricity again? As you said:

Or any totally different idea that could fulfill my purpose?

There are plenty of chat rooms, why you do not use them?

Anyways from programming perspective, if you plan to make small chat room, your idea seems nice. But as the users increment, you would be facing an overload on the server and may cause crashes on a free host.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号