PHP - Apache process use the created socket by another process - php

This is my first question on this site so I'll try to be as clear as possible.
We're building an EPP client to talk to Nominet EPP service. Nominet limits the sessions to be established by us to 5 sessions at a time. Source: http://registrars.nominet.uk/gtlds/gtld-registrar-systems/epp-gtlds
The problem that we're tackling here is we can't create a new socket every time the new Apache request comes in because of we might have 10 users that use this function at the same time.
What I'm thinking right now is we implement one script that creates 5 sockets and keeps them alive by sending hello command to Nominet occasionally (before the timeout, obviously). Then when the new process created by Apache, that process will use the exists sockets to send the command to Nominet.
The problem is I did a lot of research but can't find any way to do this, so I ended up looking for someone who knows more about this stuff.
Here is what I found but it's not what we want (not include some URL that I forgot to save while I was doing the research): PHP Threads Sharing a Central Socket Object
Anyone know how to implement the sharing socket schema? I would be more than appreciated if someone can help on this.
Also, if you know the solution in another language, please do tell. We're open for the new languages and architecture.
Thanks :)

Just finally got it works, I use a socket to solve this problem. This is how:
Process A connect to Nominet and keep the connection alive. It also opens the listener at port X.
Process B (apache process) will send the XML to process A via socket port X, then port A will forward that to the opening socket to Nominet and send XML back to process B.

Related

Rabbit MQ - How can we listen if a file has just been dropped in a LAMP server

I am new to RabbitMQ and expecting suggestion from experts here. I have a lamp based server where a PHP application is running.
I have a programming situation like from a third party remote server a file with some new data will be dropped in my LAMP server in a random manner based on some calculation on the remote server.
Whenever there is a new file dropped in my LAMP server, I need to run a few functions and update my database.
I can do it using CRON in my server but I will need to run it every minute as per need and I do not think this is the best way to do it as the duration of the new file can be a minute or can be a number of days.
I heard by someone, RabbitMQ can help me with this. My expectation is to build a system where I can establish a listener which can detect a new file has been dropped and only then will trigger the PHP function to update my database.
Please help me understand how can I take advantage of RabbitMQ in this situation.
Thanks in advance.
Sanny
you can use rabbitmq for this, if the third party remote server is sending a AMQP message to your LAMP. rabbitmq does not listen to files or anything else.
the best way to solve your problem is, create a webhook in your LAMP server to process the dropped file. (if the file dropped via http post request to your LAMP)
so whenever you have new file, the webhook can handle it.

PHP script running as Socket Server and Client simultaneously

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!

PHP Socket Help Understanding

Im trying to understand how a socket works in PHP.
Lets say I have a file called socket.php, and this creates a socket bound to my localhost on port 99.
Then I run the socket in a while loop so it's constantly connected.
is there a function in PHP to make calls to that socket while its listening?
Another question is: If I have another service such as Java running on a socket -- is it a bad idea to use PHP to connect to the socket to make a call. I ask because I could potentially be recreating new socket connections many, many times.
So is having to reconnect to a socket hundreds of times in PHP bad? Or should I re-use the same socket connection somehow? (I am thinking in terms of AJAX calls to PHP which connects to a Java Socket).
Edit: You can see the example code: https://github.com/JREAM/sandbox/tree/master/php
Im trying to communicate with in socket.php and socket_send.php -- I am leaving socket.php running and opening another console and running socket_send.php and trying to get a result into the console.
Answer to your first Question: I suggest going over here everything you need about sockets is there. Basically the function you want to use is socket_read or socket_recvfrom if using UDP.
Answer to your second Question: Sockets are just a way to send messages to services. It doesn't matter if a client is in php and the server is in Java. Think of it this way. Does it matter that you are viewing a web-page on a linux Web Server with a windows Box?

HTML5 - WebSocket in shared hosting

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

Questions about sockets in php?

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)

Categories