0

I have integrated some script on my php file. It is reading the Pusher channel and does several actions when there is a new event on the listed channel.

If I run on the browser:

http:/localhost/pusher.php

and let it open the pusher connection keeps reading, however if I close it OR run on the command line:

php pusher.php

the script opens and ends in less than one second closing the connection and not reading future entries.

Question: What is the simpler way to run the (pusher) js and keep it opened and reading under the command line?

<?php require 'vendor/autoload.php'; ?>

<html>

<script src="//js.pusher.com/2.2/pusher.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>


<script type="text/javascript" language="javascript"> var pusher = new Pusher('key'); var channel = pusher.subscribe('betfair');
    channel.bind('bets', function(data) {
      var a = data.market_id;
      var b = data.selection;
      var c = data.stake;
      var d = data.odd;
      var e = data.bet_type;


        record(a, b, c, d, e); 

    });


function record(a,b,c,d,e) {    console.log(a);
    jQuery.ajax({
        type: "POST",
        url: 'time2.php',
        data: {a, b , c , d, e}, 
        success:function(record) {
         console.log(data); 
         }
    });
     }    

</script> </html>
ace
  • 313
  • 1
  • 5
  • 19
  • You can't run JavaScript that way. Are you familiar with HTTP clients like cURL? – ficuscr Apr 28 '15 at 21:41
  • run the pusher node.js client... you can talk to it from php if needed. – dandavis Apr 28 '15 at 21:52
  • I have tried it but no succes.. At the moment I have the browser 24/7 opened with the connection live. Looking for a solution asap – ace Apr 29 '15 at 03:58

1 Answers1

1

JavaScript is almost strictly a client side language (exceptions being things like Rhino, nodeJS, etc), the things you are trying depend on the environment provided by the web browser, or more specifically a virtual machine attached to the browser that interprets the JS. You may have heard of V8 for example which is used by Chrome.

When you run the script via the command line it simply renders that JS. Nothing is actually parsing it.

You need to look at a PHP HTTP client like cURL (or maybe look at Guzzle these days).

As for a long ruinning server side process... These tasks are typically run by what people call a daemon. Maybe read this to get started on that topic: Run php script as daemon process Knowing what to Google for you should find plenty on the topic.

Community
  • 1
  • 1
ficuscr
  • 6,975
  • 2
  • 32
  • 52