Jump to content

Persistent Objects for Multiple Concurrent Users


fish123456

Recommended Posts

Hi, I was wondering if there is a way to allow multiple users to access the same PHP objects concurrently.  For example, say you want to host a chat session for 12 people.  Everyone sends messages and must be able to query messages.  Suppose message querying is done via AJAX.  Now, if I don't want to store all the messages in a database, but rather a PHP array, how can I make it so that each user accesses the same array.  This question scales to multiple chat sessions with hundreds of concurrent users.  I'm somewhat of a PHP newb, but I have high hopes.  I would encourage any discouragement anyone might have if this method is error-prone or sluggish.  Thanks!

Link to comment
Share on other sites

I guess what I'm really looking for is a global object that stays around, independent of what pages are loaded.  Something that is created when the underlying web server starts the PHP processes. 

 

Example:

1) User comes to site, they are supposed to be joining chat room 1234.

2) No user assigned to chat room 1234 has come by yet, thus the ChatRoom object for the users is not created.

3) The ChatRoom object is created.

4) Another user assigned to chat room 1234 comes by, but since the ChatRoom is already created, no further action is necessary.

 

Thus, I am thinking of something like a Factory pattern, but I don't know the method for keeping objects in memory until the users in the chat room vacate.

Link to comment
Share on other sites

This does not appear to address exactly what I'm trying to do.  Maybe I should look into a custom implementation of sessions, using a shared session key for all the users in a specific room.  That way, the data associated with the session will persist as long as I need it to and it can be created when the first user enters the room.

 

Thoughts?

Link to comment
Share on other sites

first thought is large can of worms...

 

why not just use mysql ??

 

I thought about that, but these objects will store data that is needed constantly and data that is modified constantly.  I don't want to read/write to a mysql database over and over again.  It just feels wrong.  If I called the chatroom "MadTechie's Chat" and for some reason, the users asked for the name of the chatroom over and over again, constantly hitting a DB for the name feels wrong.

 

I guess another solution along the lines of a database would be to serialize/deserialize an object (as opposed to simple data types) over and over again, but again, this feels wrong.

Link to comment
Share on other sites

If I called the chatroom "MadTechie's Chat" and for some reason, the users asked for the name of the chatroom over and over again, constantly hitting a DB for the name feels wrong.

 

thats doesn't make sence to me but.. yeah anyways good luck

Link to comment
Share on other sites

You can't keep a persistent object around in PHP.  To understand why you have to look at how PHP interfaces with a webserver.  When an HTTP request for a PHP document is received by a webserver it fires up the PHP interpreter (either via CGI or a native module)  and parses the PHP script and sends the std out to the client via the HTTP response.  Once the response is sent the interpreter shuts down and all of the PHP code is purged from memory.  You can keep an object persistent on a (per user) session basis by utilizing serialization, where the state of the object is stored on the server between HTTP requests and pushed back into memory for the duration of each subsequent request.

 

Best,

 

Patrick

Link to comment
Share on other sites

Patrick, you are crushing my dreams! :)  I went to your website and looked at your resume.  You have much more experience with PHP/MySql than I.  Based on what I've stated above, do you think it would be crazy to serialize/deserialize an object to/from a MySql DB every time I want to access the data in the object?  Keep in mind that there are multiple users accessing the same data (pseduo-)simultaneously.  Maybe I am not giving MySql its due and assuming that constantly reading/writing by multiple users is going to slow down performance.  Thanks!

Link to comment
Share on other sites

Patrick, you are crushing my dreams!

 

That's why they call me Patrick "The Dream Crusher".... but seriously...

 

If you have data which needs to be shared across sessions I think the most practical solution would involve storing your state data (messages, who's in the room, etc) in a database and then populating your array / object on each HTTP request from the database.  I actually attampted something very similar in the last PHP freaks contest (http://www.phpfreaks.com/forums/index.php/topic,129353.msg542192.html#msg542192) if you want tot have a look.  My submission was never completed but it might be enough to get you started or spark some ideas.

 

Best,

 

Patrick 

Link to comment
Share on other sites

Is there even a way to keep the object active for a single user?

 

It depends on your definition of alive.  If by alive you mean in memory, then no.  By placing the object in the session array it can persist across HTTP requests.  Although, you must keep in mind that behind the scenes the object is being serilzied, saved to the server's disk, deallocated from memory and the unserialized and stored back in memory during each HTTP request.

 

Best,

 

Patrick

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.