Socket Chat Solution?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Socket Chat Solution?

Post by s.dot »

My current chat works fantastically. It's an ajax-like solution that sends messages to a post page, to the database. Then a separate hidden page grabs the messages and appends them to the chat content area, then deletes them from the db.... once per second. All works seemlessly.

But, there's a problem. The page that grabs the messages has 3 separate queries in it. So for any given user in chat, that's 3 queries per second. Ok, that's not bad. But say there's 20 people in chat... that's 60 queries per second. Still, not THAT bad, but if it gets more... you get the idea.

I'm not too familiar with using sockets. So is it possible to do the following so that it behaves like the above mentioned script?

- Create a chat server
- Connect to it on the chat page
- fsockread() to get messages and append them to the chat area?

Of course that's just the very basics, but I would have to play around with that before I could expand on it. Thoughts on this? Would it be less server intensive IF this is possible?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

If you plan on keeping this application purely AJAX sockets aren't even a possibility.

You could cut down on queries however. Have one query check the status of the chatroom, as in, if anyone has said anything, or anyone has entered or left the room. Then, have one query for getting anything anyone has said, and one query for getting the updated chatroom members. Only execute the last two queries when necessary, based on the results of the status query. That way, the script will at most execute 3 queries, but in most cases only 1.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post by santosj »

Well, you could create a File Wrapper for it. However, the socket server would still work the same way, however depending on how you call the script, you could store the message and add it to the page.

You could also try to cache the results using XML to save on queries and allow some quick parsing.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

santosj wrote:Well, you could create a File Wrapper for it. However, the socket server would still work the same way, however depending on how you call the script, you could store the message and add it to the page.

You could also try to cache the results using XML to save on queries and allow some quick parsing.
Caching database results is only really useful when you're dealing with large complex queries. A non-conditional SELECT statement is about as lightweight as you could get. I think in his case caching results would increase overhead.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

So if I wanted to use a socket solution, how could I implement it without using ajax to append messages to the chat area?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

You would need to program something client side that supported sockets. Java, or flash if that supports sockets.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

bgzee wrote:If you plan on keeping this application purely AJAX sockets aren't even a possibility.
What? Sure it is.

Ajax is simply Asynchronous Javascript. Its clientside code that makes a query to the server, and expects XML back. (There are other asynch javascript methods that can accept JSON, HTML, raw text, and more - all in the same family as AJAX).

So the client side just takes output from the server, and updates the page. No big deal - he wrote that.

The sockets part is on the backend - to reduce the number of queries. But its not totally needed. (It would actually help in most cases though)

You only need *one* query per second (total - not per user). Each second the backend does a check for "all messages in the last second".

Thats the output it gives to the AJAX script. The AJAX script then checks the last time it displayed something, and if its longer than the last second, it displays the new stuff. Thats it.

I know what you are thinking - thats still 1 query for each user.

So cache the results of the query. Then for the next 99 milliseconds, all further queries that match will be cached - meaning you will only have one actual query per second.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post by santosj »

I was thinking more of using XML for the whole thing, but okay.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

I think some people don't understand what he is asking for when he is talking about sockets.

He is wanting to keep the connection open, that is not Keep Alive either, between the client and server in a similar manner as IRC.

This means the client NEVER queries the server for new content. It just sits there with the open connection waiting for new content to be sent from the server to either update rooms/clients or display new messages.

As it is now, even with ajax you have to poll the server for new data and that causes the server to check for new data to send from a memory cache, file cache or database cache for every client, every time a client checks for new data.

With an open socket the clients never poll the server for new data. When the server gets new data it will send it to every currently connected client. This cuts down on the overhead as you only have the one process running sending the same data to a series of connected clients.

I haven't seen anything written in PHP that will do this. I, personally, would like to find an open source project that will do it. I really don't like chat programs that have to poll the server all the time. I am using one right now that is uses an ajax client and the server php code is all file/memory based and doesn't use a database. It's a lot faster and doesn't put any real burden on the server like a php chat system would with a database.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How about Comet?
(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

hmm here's my question :-D

what's wrong with putting a script in an iframe like this..

Code: Select all

while(TRUE)
   socket_read()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Roja wrote: So cache the results of the query. Then for the next 99 milliseconds, all further queries that match will be cached - meaning you will only have one actual query per second.
Can you give me a starting point (manual, search term, code example) so I can learn how to do this? I need to know how to do this.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

I thought that flashchat script includes a socket server, I have heard people say the socket server works much better than the old polling method it onces used.

Code: Select all

www.tufat.com/s_flash_chat_chatroom.htm
pif!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

So, I think I've figured out most of it.

Firstly, I figured out that it *is* possible to write a chat server using PHP sockets. I've found some barbaric examples, so I'm modifying them and playing around with them.

I've read that it *is* possible to write a side script with PHP to utilize the chat server mentioned above. I don't know anything about this yet.. but I suppose I'll figure it out when I get the chat server scripted and working (in telnet at least). If the document I was reading is wrong about that, then I suppose a flash or java front-end wouldn't be a bad thing.

I'm kind of excited about working on this. At current my server load is way too high when 30+ people are chatting. Plus in any browser other than IE, you can tell the script is reloading in the background by the progress bar on the browser. Ugly!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

printf wrote:I thought that flashchat script includes a socket server, I have heard people say the socket server works much better than the old polling method it onces used.

Code: Select all

www.tufat.com/s_flash_chat_chatroom.htm
pif!
You can't use sockets with javascript.

The comet method looks like more trouble than its worth. It's essentially a hack to keep an http connection open and introduces a whole bunch of issues since http was never designed to run in such a way. I'd be willing the next version of http will support such a connection.

Your best bet, if you stay purely ajax, is to optimize client side polling and server side sql.
Post Reply