1

I'm very new to WebSockets and have been playing with Ratchet and gotten a simple chat room to work on my AWS EC2 instance. Now I'm trying to use another service which can run a custom PHP script (hosted on their servers) to open a websocket connection on my server and send a message.

I got the EC2 URL working, and when I run it in my browser (including on my phone, which isn't on the same network as my computer) it works. I'm simply passing a testing API key and the value in the URL for now (grabbed with PHP GET), like so:

http://ec2-22-222-22-222.us-west-2.compute.amazonaws.com/path/to/script/ws-trigger.php?key=12345&name=Bing

This works, but only because the ws-trigger.php page loads the following JavaScript and runs it from the browser:

<script type="text/javascript">
    var conn = new WebSocket('ws://22.222.22.222:8080');
    conn.onopen = function(e) {
        console.log("Connection established!");
        conn.send(<?php echo $_GET['name']; ?>);
    };
</script>

What is the easiest way for me to run this "connect to the websocket" and "send a message over the socket" via PHP? If I have to use a library I'd prefer use Ratchet since I already am, but I feel like I must be missing something because this cannot be a unique problem.

I have tried searching StackOverflow already, but have had issues with the few answers I'd already found (like phpwebsocket was giving me troubles before I moved to Ratchet, and this one isn't a PHP WS client at all)

5
  • Why do you want to use PHP as a websocket client, because PHP is stateless it would only do things on requests anyway, which is the exact opposite of the purpose of websockets (a 2 way persistent connection). I think this is an XY problem. Commented Feb 6, 2014 at 1:46
  • I'm integrating to a third party who lets me run a PHP script on their server. That script never results in browser-read HTML or JavaScript, so I cannot push the data from that service to mine via JavaScript, only PHP. I need the events they push to trigger live-pops on the screen of every user who is logged in, so WebSocket pushes seem a much better solution than Ajax polling. Commented Feb 6, 2014 at 1:50
  • Ah, I see, I can't see a way around it then. Try the answer here maybe. It seems Ratchet doesn't have a client built in. Commented Feb 6, 2014 at 1:54
  • Yeah, of those 4 answers 1 is "Ratchet" (which, as the reply says, lacks a WebSocket client), one is highly unstable, another lacks a client and the last one gives me pause, though I may try it if no one has any better solutions. I actually linked to this question in the last paragraph of my own question already. Thanks though! Commented Feb 6, 2014 at 2:10
  • Oops, well good luck, you could always write one yourself, especially if you only need limited functionality. Figure out which protocol you want to use, and find the spec for it. Commented Feb 6, 2014 at 2:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.