2

I need to set up a persistent socket connection in PHP and not sure how to achieve this. Currently when I send an SMS message I a) open a socket connection b) send a message (via SMS/SMPP) and c) close the socket connection

however I need to not open and close the connection all the time. Rather, I require

- 2 persistent connections that maintains connectivity to an SMSC (SMS centre) and reconnects when a timeout occurs. 
- One persistent connection for reading SMS and one for sending SMS.
- Automatic restart/recovery (i.e. when memory issues arise)
- Automatic looping to act as listener for incoming events such as receiving incoming delivery receipts and sms messages, as well as 'ping' (enquire link) to keep SMPP connection alive.

UPDATE: Was wondering if anyone had achieved the above using the following: https://github.com/shaneharter/PHP-Daemon

user1746582
  • 579
  • 1
  • 9
  • 20
  • http://php.net/manual/en/function.shm-put-var.php there's a nice read about this in the comments. – sinni800 Mar 06 '13 at 09:51
  • I have this requirements done in this project: https://github.com/nimf/phpesme/ We have kind of watchdog script which ensures rx, tx and handler processes are always running and will be restarted on exit. – Yuri Golobokov Mar 27 '13 at 07:35
  • thanks Yuriy. A few weeks ago I would have seriously considered solutions such as the one you have provided a link for. However in recent weeks I have upgraded all PHP/SMPP to Java/SMPP and have not looked back :-) – user1746582 Mar 27 '13 at 15:49
  • @user1746582 I think the PHP-Daemon library would work great for you. I've used it in a few projects, from simple daemons that read jobs from a queue and process them in-line to recently a much more complex application that used background workers. (Takes an incoming socket connection, listens for a job request, then farms it out to a background worker to keep the main process unblocked. Really its the best you could hope for in a language like PHP without threads) – Shane H Apr 06 '13 at 20:19

1 Answers1

1

Function pfsockopen seems to have capabilities you are looking for. Check out this question - PHP pfsockopen in a session.

===

A personal observation on your implementation. I am assuming that PHP code will be triggered by an incoming request and all other times, SMPP client will be inactive. This may not be quite suitable for SMPP for few reasons:

  • SMPP clients are required to send back response PDU(s) for all the inbound PDU they receive from SMSC.
  • One of these PDU(s) is enquire_link which is used to keep a connection alive. If a SMPP client fails to return enquire_link_resp PDU, the connection will be dropped. You may experience more of these. Using pfsockopen will not solve this problem.
  • If you want to offer delivery confirmation, the SMPP client must respond to the SMSC.
  • If a SMPP client fails to send response PDU for the PDU it recieves for SMSC (if it reads the bytes from the pipe), the SMSC may resend some of those PDU(s). This opens up possible risks of creating unnecessary hit on the SMPP client machine.
Community
  • 1
  • 1
Wahid Sadik
  • 908
  • 10
  • 24
  • thanks for your feedback. I am aware of pfsockopen and also SMPP requirements. I have noticed this link which supports create long running server processes in PHP: https://github.com/shaneharter/PHP-Daemon Be interested in your thoughts on this. – user1746582 Mar 06 '13 at 09:39
  • I have solved this problem with Java but not with PHP. At work, we use one thread for each bind. Each thread is busy waiting for incoming bytes streams and responds to all incoming PDU. With PHP-Daemon (disclaimer: I have not used it), you can loop and achieve the same result; you can can detect dropped connections and re-establish them. This may not work where the SMPP client is required to send enquire_link (or need to do some proactive stuff). You may need separate thread to do that. Perhaps, http://tinyurl.com/cyxclfx can provide more info. – Wahid Sadik Mar 06 '13 at 20:05
  • it's interesting you mention Java. I'm coming to the realisation that PHP trying to handle this would be messy. have you seen or used Twitter's CloudHopper by any chance? As we know Java, we are now considering using this to address my requirements above. Though can't find any documentation on the methods apart from examples provided. If you put your comment suggesting how you solved my problem in Java I may accept it as an answer :) – user1746582 Mar 07 '13 at 08:32
  • At work, we use our own implementation. I used [Logica](http://opensmpp.logica.com/introhtml/men u.htm) in the past; it can work for you if you know the limitations. I have heard of [CloudHopper](https://github.com/twitter/cloudhopper-smpp). It is backed by Twitter and is under active maintenance. It seems the logical choice to me. – Wahid Sadik Mar 07 '13 at 21:12
  • There are not much examples for CloudHopper. You can start with [Logica Java Test Application](http://opensmpp.logica.com/CommonPart/Download/download2.html#javatest). It has full examples of sending/receiving/enquire_link/and everything else. You can later on modify the code to hook CloudHopper and replace Logica. – Wahid Sadik Mar 07 '13 at 21:24
  • Thanks for the Logica link, I did notice that also. I will look into testing with this. Many thanks. It has been noted this requirement is best suited to Java, Perl, Python, Ruby and not PHP. As we know Java best, we'll likely go this route too :) – user1746582 Mar 08 '13 at 08:45