forum.webdiplomacy.net

webDip dev coordination forum / public access todo list
It is currently Mon Sep 25, 2017 12:49 am

All times are UTC




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Instant Messaging
PostPosted: Sun Oct 11, 2009 5:38 pm 
Offline

Joined: Wed Sep 09, 2009 2:37 pm
Posts: 156
The next big coding project I'm going to take on is instant messaging. I already know how to do it via javascript, but I know how K is always talking about database efficiency, so I might need help with that.

The "easy" way to do this is to query the database every 3 seconds at the most. This obviously creates a lot of overhead when you have 100 people all on at the same time.

One way I thought of getting around this is to create a file on the server like $memberID."-".$memberID2."new.txt" that is pushed every time the new messages flag is set in the database. Then when the new messages bit is unset, it deletes the file. That way, I just need to have a server-side "file_exists" check every 3 seconds, and I don't need to query the database at all.

Does that sound like it would work? Any other ideas?


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Mon Oct 12, 2009 3:42 am 
Offline
Site Admin

Joined: Sat Jun 28, 2008 6:24 am
Posts: 892
I think that'd work well, as I said elsewhere trying to do it a less smart way got my (relatively dead-quiet) server taken down, so it's important to keep these in mind. I'd suggest adding a secret token to the filename to prevent others from seeing if someone is being messaged, but that's not a big deal. Also it might be an idea to actually store the new messages in the file itself, so the chat can be updated directly from the file, but that's a little more complex for a little gain so it's also not so important


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Mon Oct 12, 2009 7:15 pm 
Offline

Joined: Wed Sep 09, 2009 2:37 pm
Posts: 156
Yeah, I got the code completely working with just regular database queries every 2 seconds. When just my friend and I were testing it, we took up 13 percent of dreamhost's CPU. I told him to close his browser pretty quickly after I saw that!

The secret token is a good idea, I'm just not sure how I can do it... Maybe a token column in wD_Members that's stored in the class.

I'm hoping to get something releasable sometime this week. I also already have an ajax order input script that needs cleaning up (input orders without refresh); I was going to release them both at the same time. The input orders script actually takes less CPU because it only queries the database for the new dropdowns, not the entire page.


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Mon Oct 12, 2009 7:34 pm 
Offline

Joined: Wed Sep 09, 2009 2:37 pm
Posts: 156
I just read your other post and I can see how client-side javascript may be better. I basically cut the original order code out of board.php and but it in orders.php. Then I put an onChange in the select box to reset the innerHTML of the order box. It also doesn't query the database for menus already cached into session variables. It's not ideal, but then again, I only have about 12 users :) But thanks on the heads-up for that in the future.


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Wed Oct 14, 2009 10:40 pm 
Offline

Joined: Wed Sep 09, 2009 2:37 pm
Posts: 156
Bingo, I got it (the chatbox update anyway).

I tested with 4 users, using firefox as well as IE, all with 1 second updates (checking a static file on the server, updated only when messages are sent). Resources while all four were connected didn't even blip on the server resources list (compared to 10% CPU use with 2 users using 100% database queries).

The initial chat display is loaded from the database, but then the client queries tokens/$token-$msgCountry (or global-$gameID) every second. The file size is generally around 130-150 bytes per line chat line, so even on a 56k modem the 1 second update will pull the file in half a second. If the static file is different than what is displayed, the client updates the chat screen, otherwise, the client does nothing.

All I have to do is throw a quick messagesRead query when the chatbox updates and there you go, instant messaging. I'm hoping to have it done by the end of the week.


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Wed Oct 21, 2009 1:14 pm 
Offline
Site Admin

Joined: Sat Jun 28, 2008 6:24 am
Posts: 892
Hey gilgatex could I get this code off you? I've just added JavaScript order generation so these two patches together would go quite nicely


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Wed Oct 21, 2009 5:03 pm 
Offline

Joined: Wed Sep 09, 2009 2:37 pm
Posts: 156
I haven't finished it completely yet. I can give you what I have (just the messaging/caching part and the javascript tab swapping), but I haven't gotten to notification updates yet for tabs not in focus.

Bottom line is that it works, but it's incomplete.

Also, for some reason when I was putting in orders in the javascript order code, it kept asking if my units wanted to move/support via convoy or land (even if it was a fleet order). I don't know how to recreate the error.


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Fri Oct 23, 2009 12:29 am 
Offline

Joined: Wed Sep 09, 2009 2:37 pm
Posts: 156
I spent the last 24 hours working hard on this and I'm about 98% satisfied with it. There is more than can be done, but it works, and it works well.

Here's the link: http://careyj.com/dippatches/iming.tgz

To Install: decompress to your base directory. Import install/iming/iming.sql and apply the patch: install/iming/iming.txt. I tried these three steps on a "latest" install (10/22/09) and it immediately worked. It will very likely work with 0.93 release and will probably work with 0.92 and 0.91.

Here's basically how it works (thoroughly tested in FF and IE, but not Opera):

Timers set (when the page loads):
1) Every 1 second: refresh the chatbox with tokens/$token-country (or tokens/$gameID-global) so it gives the appearance of instant messaging. If there is no change, it doesn't update the client. If there is a change, it updates the viewport and scrolls to the bottom.
2) Every 3 seconds: check tokens/$token-msg-$gameID; this file (if it exists) contains a colon-delimited list of powers the current Member has messages from. If it finds the file, it pulls it, and adds a new message icon
3) Every 10 seconds: check tokens/online-$gameID; this file contains a colon-delimited list of all the powers currently viewing the Game. Note: This is different than the "online users" shown on the home page. This will only show people viewing the game (or people that have closed a browser without leaving the game). On every single page that is not board.php, the power is removed from this static list, so others know that he is not looking at the game. When a user is removed from wD_Sessions, they are also removed from the static online files for all their games.

Actions:
1) Send a message: this can be done either by hitting the "Send" button, or pressing the enter key. When this happens, an ajax calls a small script (basically a shorter version of the entire board.php) to update it in the database as well as in the token directory (in both the chat token as well as the newmessage token). The message icons and chatbox is updated within the next update automatically.
2) Change tabs: When you click a new tab, the entire chat box is disabled/greyed out with a 'loading' message, the chat refresh update is halted, and a small script (same as above) pulls just the information needed to display the new tab. When the new tab finishes loading (anywhere from instantly to a second or two), it overwrites the div that contains the old tab.

Known minor issues:
- The "online" icon does not go away until you refresh the page or select another tab, but it does show up within 10 seconds of another Member joining the game.
- The "new message" icon at the top of the page will continue to show in a global conversation. It won't go away until you refresh or change tabs. This probably isn't that hard of a fix and I plan on getting to it later.
- No extensive speed testing has been done. I've had a few of my friends all in one game and my number of processes were high, but the use of system resources were very low (CPU never spiked more than 1% and memory usage never spiked more than 0.2%).
- I have only tested this with FF3.5 and IE7.0. I would suggest at least trying it with IE8.0 and Opera before setting it up on the main site.


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Fri Oct 23, 2009 1:41 am 
Offline

Joined: Wed Sep 09, 2009 2:37 pm
Posts: 156
My brother just confirmed it worked on his Android Mobile Phone


Top
 Profile  
 
 Post subject: Re: Instant Messaging
PostPosted: Fri Oct 23, 2009 12:53 pm 
Offline

Joined: Wed Sep 09, 2009 2:37 pm
Posts: 156
If you downloaded before this post, download again, I fixed the gamemaster not updating correctly (the one thing I forgot to test!)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group