Jump to content

Flash and PHP


mark123

Recommended Posts

Hi,

 

I'm looking to create a very small .swf which displays how many posts/topics/members there are in my forum - but Im looking for it to update in real time.  So as posts are made on the forum, if someone is viewing the homepage they see the number update 'live'

 

I'm wondering if anyone can tell if this is hard to do?  I'm fine with PHP and am just starting to learn flash, but so far only have done minor animation.

 

Would it work by embedding a PHP script in the flash to run a query on the db to count the posts, then echo it in the flash.  But how would I get it to update live?

 

If anyone has any ideas on how I can get the live update bit to work, I'd appreciate it.

 

Many thanks

 

mark

Link to comment
Share on other sites

My knowledge of flash is basically nonexistant but I believe you can make a call to a url and get the output?  What you'd basically need to do is an AJAX call but using flash instead of javascript.  Have your swf call the php script, every second maybe, and the script would output whatever you needed to know in a form you can then parse in flash.

 

You might want to look up something called AMFPHP - not sure what it does but I heard that it is good for working with Flash and PHP.

 

The tutorial below is for a Flash-based email form, but it gives you the basics:

http://www.kirupa.com/developer/actionscript/flash_php_email.htm

Link to comment
Share on other sites

I would probably just do what cagecrawler said because you would need a dedicated server for what I'm about to say.  And because his way is easier to code too ;p.

 

 

A different way would be sockets which could be written to when ever the data changed..... The basic idea would be:

 

-client side (flash) connects to the server.

 

-The server sends the current data

 

ok either a or b would happen next:

 

a. If the data was coming from something that could tell the socket server app that it had changed, it would wait for that, and when it recieved that signal it would send a new value to every socket connected to it.

 

b.  If the data was coming from something unpredictable, such as a database (unpredictable in most circumstances), then the socket server would have to check every x seconds and then if the data had changed it would send the update data.

 

Obviously method B brings back the problems with the website request, but it would (hopefully) be on the same server cutting down bandwidth and speed, and the same script wouldn't have to run hundreds of times if you had hundreds of people viewing the page (but, you would have to write to 100 sockets.  And you would have to have the server app always running....)

 

I think flash can use sockets, but I don't remember for sure.... I know Java can, but I dislike Java.

 

To mark123:

 

You could probably do yours with some AJAX since it would theoretically be small amounts of data changing, it wouldn't take much bandwidth or time (hopefully).

 

You would simply load the page with the original data and then every X seconds check a script and update the values on the page based on that.

Link to comment
Share on other sites

Let me explain my chatroom kinda script.

 

It's set with databases and what happens is that it'll call the database, take latest message to the messages up until the user just came in, and just re-update the div. My problem is that if this happens on a busy server, it gets really slow in the update, it'll call, but takes over 5 seconds for data to come back. I thought it may have been doing it in html that may have been bad. So I was hoping flash may work faster. Anyone have any solutions?

Link to comment
Share on other sites

I would return the data in a JSON object or just an array since you could generate the mark up client side.

 

Also, from your example, I think you are doing this, but I'm not sure so I'll ask anyway:

 

Are you getting the messages since the last update or all of the recent messages?  If you pass along a timestamp to compare to the database, you might only have to send 3 new ones, not 25 or how ever many you show at a time.

Link to comment
Share on other sites

Well, that's my problem. I don't understand how to get an array to pass through AJAX if you can do that.

 

If you can, then I can do the thing where I'm not taking as much from the database by doing something like a multidimensional array with ID=>value, I do not know how to do that.

Link to comment
Share on other sites

How could I implement this if I wanted to use XML or what? I mean, I like to rely on the database so that I can insert the past 5 messages to get the new user up to speed on what he missed.

 

But how would you set it up? I'm lost at what you're aiming at. Sure, XML and JSON are text-based documents, but.. so what?

Link to comment
Share on other sites

Well, let's say you're sending the client back

 

<font color="red">Corbin:</font> Hey, what's up?<br />

<font color="red">kratsg</font>Not much<br />

 

You could instead send back:

 

{"posts": [{"auth": "Corbin", "content": "Hey, what's up?"}, {"auth": "kratsg", "content": "Not much"}]}

 

Ironically enough, the second method in this case is actually 3 characters longer, but if there was more html markup, this way could save data.

 

The a downside to JSON is, it's interpretted client side meaning that it takes longer for the browser to process.

 

Oh, an example parsing function:

 

function Parse(json) {
//json = eval("json = '+json+';");
var json = eval('(' + json + ')');
nposts = json['posts'].length;
var ret = '';
for(i = 0; i < nposts; i++) {
	ret = ret + '<font color="red">'+json.posts[i].auth+':</font> '+json.posts[i].content+'</br>';
}
return ret;
}

 

Obviosly this extra function is once again longer than the html would be, but the JS would be cached, meaning if 10 characters were being saved on average a message, the script would be covered in bandwidth with very few messages.

 

Unless you're applying a ton of HTML to each message, I personally would stick with HTML, since JSON would cause a processing hit server and client side (most likely unnoticable on both ends), and it runs the risk of using more BW x.x.

 

 

Link to comment
Share on other sites

Wow, ironically enough... xD

 

How is the eval() function using that as a php code in this case? I'm a little lost with JSON :-P

 

And I still have a question about passing arrays through AJAX (if we could do this, it can help with returning only the NEWEST posts)

Link to comment
Share on other sites

The eval function is a javascript function, not a PHP one (well, it is a PHP one, but not in that context ;p).

 

To parse an array recieved via AJAX (this has the same output as the JSON method):

 

data = "[['Corbin', 'Hey what\\'s up?'], ['kratsg', 'Not much']]"; //preset some data since I don't feel like writing a bunch of AJAX crap
//note: that doesn't actually create an array, it creates a textual layout of an array as that would be what would be returned from a php script

function Parse(arr) {
     //var arr = eval(arr);
 eval('var arr = '+arr);
     anum = arr.length;
     var ret = '';
     for(i = 0; i < anum; i++) {
          ret = ret + '<font color="red">'+arr[i][0]+':</font> '+arr[i][1]+'</br>';
     }
 return ret;
}

 

It's also worth noting that the array string is 35 characters shorter than the HTML generated, and that the Parse() function is 267 characters long.  Bandwidth is of course in bytes and not characters, so the encoding would of course come into play, but the bytes would still be proportional.

Link to comment
Share on other sites

I have done stuff similar, with updating live counts,

 

most simple solution, is,

 

get PHP to get the number out of sql and write a text file every time a change is made called for example

 

number.txt.

 

repeat this every time the number is updated by overwriting the text file

 

text file content:

 

number=42    // for example

 

then have a swf open the text file, make it a variable every 20 or so frames,

 

then simply have the movie display the variable on screen

Link to comment
Share on other sites

Well, I could post mine, it uses PHP, AJAX, and MySQL. It's split up over different pages to coordinate different ajax calls. Only two ajax calls, one for the messages, and one for the current users.

 

The problem is that it may lag, and while everything works, I'm hoping to find a solution to increase speed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.