I have developed my own XMPP PHP library which I use to communicate with external XMPP server which is not mine.
After initial auth I am sending empty presence stanza <presence/>.
I have an open socket socket connection to that server which is fetching server response, if any.
I have also implemented my library within a corporate app. I have set the app resource to be one thing, and app running from terminal to other, so I can distinguish between the two.
What I've noticed though if I have both running in parallel, I am constantly getting these responses:
<presence from="a#b.com/app" to="a#b.com/terminal"/>
<presence type="unavailable" from="a#b.com/app" to="a#b.com/terminal"/>
These never seem to stop unless I kill one of the two running instances. I am not sending any request which would require server to respond with unavailable presence so I'm not quite sure what is happening here and how can I resolve it?
Related
For a new project I need to implement remote desktop protocols. The addresses of the remote need to be secured and may never get sent to the client. After a lot of research and some tests I found Guacamole, which also has a Java client. The project is designed as an API though, so I started porting some bits of the Java client example to PHP.
The use case will be the following:
User logs into my service (Laravel application)
WebSocket connection establishes to a constantly running PHP script (using HOA\WebSocket)
Upon authorization a TCP socket needs to be established to the Guacamole Daemon
Commands coming via WebSocket need to be directed to the Guacamole Daemon and vice versa
What makes this complicated is the fact that the application needs to be able to serve multiple clients simultaneously. Multiple TCP sockets need to be established and multiple WebSocket connections need to be managed all at once.
For my simple test I opened the socket via fsockopen and then looped to wait for data. With this I obviously can't listen to multiple sockets at once (at least realistically), but I stumbled upon the React Socket Client library:
Think of this library as an async version of fsockopen() or stream_socket_client().
This sounds like it is what I need, but then again, I'm using HOA and its WeSocket server, which apparently also runs in a loop (when invoking WebsocketConnectionHandler->run()).
Should I even be using React's Socket Client or should I try to use HOA's Socket library instead (seeing as I'm already using WebSocket from that)? Are React and HOA even compatible in their event loop, so could I listen to WebSocket clients and a TCP connection at the same time?
If so, could anyone give me some hints or examples on how to get started with coupling these two? Thanks!
I'm currently developing a PHP application that is going to use websockets for client-server communication. I've heard numerous times that PHP shouldn't be used for server applications because of the lack of threading mechanisms, its memory-management (cyclic references) or the unhandy socket library.
So far, everything is working quite well. I'm using phpws as the websocket library and the Doctrine DBAL to access different database systems; PHP is version 5.3.8 . The server should serve a maximum of 30 clients. Yet especially in the last days I've read several articles stating the ineffectiveness of PHP for long running applications.
Now I'm not aware whether I should continue using websockets with PHP or rebuild the entire serverside application. I've tried Python with Socket.IO, though I did not get the results I expected.
I guess I have the following options:
Keep everything as it is.
Make the application use Ajax in combination with Socket.IO - e.g. run a serverside script that invokes the client's ajax calls when data is submitted to the server.
The last point sounds quite interesting, though it would require some work .. Would it be a problem for servers to execute all the clients requests at one time?
What would you recommend? Is the problem with PHP's memory management (I'm using gc_collect each time a client sends data to the server) still valid? Are there other reasons beside the obvious reasons (no threading, ...) for not using PHP as a server?
You can try running your socket.io on a node server on another port on your server (that is if you are not using a hosting plan like goDaddy).
I am using it and the performances are really satisfying.
I have an apache server on the port 80 serving my php files, and my server-client communications are done using a Node.js server running socket.io on the port 8080 (dev) or 843 (prod).
Node.js is really light and has great performance, but you need to run it as a server. Nodejitsu.com is a hosting solution that has the websocket protocol available and is on beta, so it is still free for now. Just note that you need to listen on the port 80 with socket.io, this is a limitation from theyr network.
If you want your pages all to be accessed on the port 80 then you will need a reverse proxy like varnish .
I hope that helps! Have a nice day.
Are there other reasons beside the obvious reasons (no threading, ...)
for not using PHP as a server?
Yep, lots of socketfunctions are incompatible with each other and it's a hell to debug.
i tried something similar myself and quit frustrated sind every function i thought would make sense didnt do what i expected
Let me clarify
I played with phpwebsockets and see that it requires a websocket server to run forever/constantly to keep the states etc in the memory is there any way to run websockets when there is no way of running a server on the server side?
You can used a hosted realtime service, such as Pusher who I work for. Here's comprehensive list of realtime hosted services.
There will always have to be a server of some sort listening constantly. For example, HTTP traffic is delivered by a web server (Apache, Nginx, Lighttpd, etc.) that is running constantly.
It is possible for a web server to spawn a new PHP based process, then hand the client's connection over to the new process. However, none of the major web servers currently do this, and the paradigm for web server modules that route WebSocket traffic is to send the connection over to an existing, constantly running server.
You would have to write your own custom web server in order to keep from running a WebSocket server constantly... and then, really, what's the point, since you're still writing your own constantly running server?
I used to have a small chat app(which was almost working), that uses PHP, jQuery and MySQL. The volume of users is very small (only my friends uses it). I used long polling method for this.
And now, I am thinking about using HTML5 Websockets for this, because it is a lot more efficient. And also most of my friends are using Google Chrome(which already supports HTML5). I have gone through some tutorials that talks about HTML5 websockets. And I have downloaded the phpWebSocket from github. I have gone through the code. But the readme file says that the PHP page that listens to incoming connections should be run using "PHP -q" from commandline. So, I have searched what this "q" flag would do. And I found that it runs the page in quiet mode. So, when I run this in quiet mode what is happened ? It would run endlessly ? Will this running process affect the system resources ?
This PHP page should run the entire time. Then only the connections could be accepted. Isn't it ?
I am having a shared hosting package with HostGator. And they allow cron jobs too. And my present chat app(that uses long polling method) inserts all the messages to database. When the user polls, it would search for any new messages from the database and then output them (if any).
So, I am bit stuck here. :(
It should be run from the command line because as you suspected, it is intended to run endlessly. It binds to a socket on the server and listens for incoming connections. It can't be reliably run from the browser.
The "-q" option tells it not to output any browser headers such as X-Powered-By: PHP or Content-Type: text/html
It will consume as much memory as PHP requires as long as its running. Your memory footprint on startup with no clients will vary between configurations. The more connected clients, the more cpu, memory and socket descriptors you will use. It uses select so it is efficient socket handling.
Also, since you're on shared hosting, you probably won't be able to use it because your user will most likely not have the ability to bind to a port and listen for connections.
As you can see in the demo, the URL to connect the WebSocket to is ws://localhost:12345/websocket/server.php. Unless you have a webserver capable of using WebSockets, you will have to run something like phpWebSocket that acts as a server and listens on a port other than 80.
Hope that helps.
The shared hosting package for HostGator does not allow clients to bind to local ports for incoming. This might be part of the problem.
http://support.hostgator.com/articles/pre-sales-policies/socket-connections
1- Let's say my computer ip address is 111.11.111.11, and the server that my php script is on is 222.22.222.22, so if i access and run the php script that is on the server and start a socket server, which ip do my clients need to connect to?
2- Is it possible to have a socket running on php which keeps reading, and responding to the clients until I close the browser, So basically what i'm trying to do is to start a socket which keeps reading, and accepting clients, and keeps communicating with them multiple times with each.
thanks for the answer, but i think i didn't explain well enough on my question 2, so let me make it easier:
Is it possible to create a chat server using php? because the point i was getting into was if it's possible to accept multiple clients and keep them alove.
222.22.222.22. But it sounds like you are starting up a socket server in response to a HTTP request. Probably, that won't work as intended, since the PHP interpreter terminates after the response is sent. If you had permissions, you could fork a separate socket server process, but I don't know what that would accomplish.
No. Even if you kept the interpreter running, there is no way to tell when the browser closes. The closest you can get is determining the browser (as determined by cookies or IP) stops communicating with you.
1- 222.22.222.22, your server's IP.
2- When a visitor arrives you can spawn a 'socket process' and implement a client side 'heartbeat' application using JavaScript/AJAX, but that implies you running the socket backend script (possibly) for a long time, which may cause problems (Like having a lot of PHP processes open, depending on the way your web server is set up this may cause problems)