Click to See Complete Forum and Search --> : PHP RMI capabilities?
BuezaWebDev
09-15-2006, 03:37 PM
Is this possible? I've done a project in Java involving data (XML) being shared between distributed systems syncronously using RMI, but I was wondering if it's possible to do this using PHP?
What I'm trying to achieve is a syncronous connection to an object which will have many clients connected to it (different people) and they can all share the same data syncronously.
AJAX can do this this, but it's always interacting with the database. I don't want to interact with a database--is there some way to keep a PHP object alive to receive and release data syncronously to clients (users on the system).
Ultimately, I want to make it so that every client connects to a "live" PHP object, and then that PHP object connects to the database. Kind of like a manager class in a typical RMI system.
chazzy
09-15-2006, 05:45 PM
PHP has shared memory capabilities http://php.net/shmop
It's inherently faster than java since RMI requires serialization to pass between the VM's but since java is faster than PHP, it's a toss up.
BuezaWebDev
09-15-2006, 07:13 PM
That was an interesting read, since I didn't even know about the shared memory functions--but I was wondering if there was a way to keep a PHP object alive to receive and release data syncronously... When a person loads the page, an object is instantiated...let's say a game like Contra (everyone has played that game :p).
GameManager class (manages player/enemy locations)
Player class (two objects will be instianted, since it's a two player game)
Enemy class (many objects will instantiated, since there are many enemies you have to shoot)
Game Manager will manage all the locations and interactions between the players and the enemies.
Is there any way to keep the GameManager object alive to receive/distribute data syncronously with the clients?
Since it's "bad practice" to use syncronous calls with a XMLHttpRequest object, I was thinking perhaps a live PHP object can act as the syncronous middle tier and then connect to the DB.
NogDog
09-15-2006, 07:17 PM
Just to clarify for my understanding: are you trying to share one object among multiple users, or persist an object across multiple requests for the same user?
BuezaWebDev
09-15-2006, 10:13 PM
Just to clarify for my understanding: are you trying to share one object among multiple users, or persist an object across multiple requests for the same user?
That's a good question--It's both.
Persist an object across multiple requests and multiple users. The manager object can allow multiple clients to make multiple requests to it.
chazzy
09-16-2006, 11:00 AM
you're looking for two different things and they need to be implemented separately.
as far as i am aware, php objects will only stay alive while the script is running, so you need to create a daemon script to run.
BuezaWebDev
09-16-2006, 01:00 PM
Any alternative situations/setups that I can use to achieve this goal?
Perhaps instead of a constant connection, I could set a timer for each client to retrieve updates from the manager class? This would not achieve true "real-time" though.
JohnPearcey
11-18-2010, 08:40 AM
I've had the same problem which I've recently solved if anyone is interested. I have a huge amount of Java code and wanted to use it when writing websites for which I prefer PHP. So, I've written a middleware server which I'm thinking about distributing under GPL. I've written both PHP and Java clients to communicate with it using RPC style calls. The PHP can work stand-alone or on web pages. The middleware maintains state/session etc.. It is being used in two websites currently. I did this because the php/java project seems now unsupported and I think tomcat is rubbish. I am looking at JBoss at the mo but don't see any PHP support so this would require writing the IIOR stuff. I did also find a small php-java lib that someone wrote using cut-down xml calls, quite fast too. If anyone wants any help or advise, or can help me stop re-inventing wheels, drop me a line: john at pearcey dot net.
svidgen
11-18-2010, 11:39 AM
If you're looking to avoid database interaction (though not necessarily a bad solution), you might consider installing APC: http://www.php.net/manual/en/book.apc.php
At each page load, check for the object in the cache -- if it's not present, initialize it however you need to. I'm not sure how you'd handle modifications to the shared variable (which could be an object) though. I have no idea offhand whether PHP performs any locking, provides any semaphore or mutex structures.
I would actually recommend just using the database for this. It will store commonly accessed records in memory, making access to them pretty efficient. And you can then lean on the database locking and transaction mechanisms -- even down to single attributes of the shared object, if implemented in a somewhat normalized form.
ADDENDUM: A database will also allow objects to be shared across web servers -- something that a caching mechanism won't facilitate very gracefully. Though, I suppose if you encounter the need for multiple web servers, you may already be encountering the need to (re)architect your application to avoid globally shared objects.
ANOTHER ADDENDUM: Installing APC will also [drastically] improve the general performance of your app. Java may typically outperform PHP on account of using precompiled opcodes, eliminating the need for complex parsing and optimization at runtime. Java may also keep the application resident in memory, which is significant. (enter my ignorance of Java server technologies). However, APC will give PHP similar advantages by keeping PHP opcodes resident in memory after the initial load of each used file.