fish123456 Posted May 15, 2007 Share Posted May 15, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/ Share on other sites More sharing options...
MadTechie Posted May 15, 2007 Share Posted May 15, 2007 an array won't work unless you created the array in a php file on the fly (but then again you could just write to a file!) Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253546 Share on other sites More sharing options...
fish123456 Posted May 15, 2007 Author Share Posted May 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253550 Share on other sites More sharing options...
MadTechie Posted May 15, 2007 Share Posted May 15, 2007 was their a question in that last post!! Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253554 Share on other sites More sharing options...
fish123456 Posted May 15, 2007 Author Share Posted May 15, 2007 was their a question in that last post!! Is there a way to do what I want to in PHP? Is there a way to create global objects that persist indefinitely? Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253560 Share on other sites More sharing options...
MadTechie Posted May 15, 2007 Share Posted May 15, 2007 yes, look in the manual Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253564 Share on other sites More sharing options...
fish123456 Posted May 15, 2007 Author Share Posted May 15, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253585 Share on other sites More sharing options...
MadTechie Posted May 15, 2007 Share Posted May 15, 2007 first thought is large can of worms... why not just use mysql ?? Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253590 Share on other sites More sharing options...
fish123456 Posted May 15, 2007 Author Share Posted May 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253603 Share on other sites More sharing options...
MadTechie Posted May 15, 2007 Share Posted May 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253611 Share on other sites More sharing options...
fish123456 Posted May 15, 2007 Author Share Posted May 15, 2007 Anybody else understand what I am talking about? Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253615 Share on other sites More sharing options...
utexas_pjm Posted May 15, 2007 Share Posted May 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253617 Share on other sites More sharing options...
fish123456 Posted May 15, 2007 Author Share Posted May 15, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253637 Share on other sites More sharing options...
utexas_pjm Posted May 15, 2007 Share Posted May 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-253648 Share on other sites More sharing options...
fish123456 Posted May 15, 2007 Author Share Posted May 15, 2007 Thanks for the info and the links. So basically you create an object to do something, then when that something has completed, the object is destroyed. Is there even a way to keep the object active for a single user? Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-254011 Share on other sites More sharing options...
utexas_pjm Posted May 16, 2007 Share Posted May 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-254061 Share on other sites More sharing options...
fish123456 Posted May 16, 2007 Author Share Posted May 16, 2007 OK, thanks for all the help Patrick. Quote Link to comment https://forums.phpfreaks.com/topic/51481-persistent-objects-for-multiple-concurrent-users/#findComment-254081 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.