Missing code in get-stream tutorial

688 views Asked by At

Updated code using js for realtime listening, but still the same issue that nothing happens.

I am trying to achieve the following:

1) 'Eric' sends a feed with the following code inside file eric.php:

   <?php
      require_once 'PATH/TO/MY/vendor/autoload.php';

      $client = new GetStream\Stream\Client('mykey', 'mysecret');

      // For the feed group 'user' and user id 'eric' get the feed
      $ericFeed = $client->feed('user', 'eric');

      // Add the activity to the feed
      $data = [
        "actor"=>"eric",
        "verb"=>"like",
        "object"=>"3",
        "tweet"=>"Hello world"
      ];
      $ericFeed->addActivity($data);
    ?>

In alternative, I could use the JS instead, but this does not solve the problem either:

     <script>
     // Initialize the client with your api key, no secret and your app id
     var client = stream.connect('key', null, 'id');
     // For the feed group 'user' and user id 'eric' get the feed
     // The access token at the end is only needed for client side integrations
     var ericFeed = client.feed('user', 'eric', 'key');
     // Add the activity to the feed
     ericFeed.addActivity({
     actor: 'eric', 
     tweet: 'Hello world', 
     verb: 'tweet', 
     object: 1
   });
  </script>

2) Jessica, who is logged into her page jessica.php wants to be notified of Eri's feed. Following the tutorial, this is what I use to achieve this:

 <?php
   //load Pusher or stream libraries
   require_once '../../../vendor/autoload.php';

   $client = new GetStream\Stream\Client('mykey','mysecret');

   $client = new GetStream\Stream\Client('b5qhgudtn6my', '4qkfwmsvfrprm3zp5smuxfnvrcb227f8sf49pt7mene3ra8kmz2mgk3tkne4nync');
            // For the feed group 'user' and user id 'eric' get the feed


        //Stream
        $jessicaFlatFeed = $client->feed('timeline', 'jessica');
        $jessicaFlatFeed->followFeed('user', 'eric');
        $response = $jessicaFlatFeed->getActivities(0, 3);

        $ericFeed = $client->feed('user', 'eric');
        $token = $ericFeed->getToken();
 ?>
 <div class="12u 12u align-center">
   <h3>RECEIVE UPDATE HERE</h3>

   <script type="text/javascript" src="/stream-js-master/bower_components/getstream/dist/js/getstream.js"></script>
    <script type="text/javascript">
        var client = stream.connect('b5qhgudtn6my', null, '19328');
                // Javascript client side feed initialization
        var ericFeed = client.feed("user", "eric", "<?php echo $token;?>");

        // Listen to feed changes in realtime
        var promise = ericFeed.subscribe(function(data){
            alert("WORKING!");
            console.log("Working!!!", data);
        });
        // Add an activity when the websocket is ready
        promise.then(function() {
        ericFeed.addActivity({actor:"eric", verb: "tweet", object: 2, tweet: "AWESOME!"});
        });
      </script>
   </script>
 </div>

At this point i would expect the alert message to pop up in Jessica's page, but it doesn't.

Below I report the code from the tutorial at

 https://getstream.io/get_started/?language=php

Step 2/5 php: Flat feed:

    // See https://github.com/tbarbugli/stream-php for install instructions
// Initialize the client with your api key and secret
$client = new GetStream\Stream\Client('key', 'secret');
// For the feed group 'user' and user id 'eric' get the feed
$ericFeed = $client->feed('user', 'eric');
// Add the activity to the feed
$data = [
    "actor"=>"eric",
    "verb"=>"like",
    "object"=>"3",
    "tweet"=>"Hello world"
];
$ericFeed->addActivity($data);

NOTEs: This code is what I have put inside eric.php of my own code.

3/5: php: Follow

// Let Jessica's flat feed follow Eric's feed
$jessicaFlatFeed = $client->feed('timeline', 'jessica');
$jessicaFlatFeed->followFeed('user', 'eric');

NOTEs: This is wht I put at the top of jessica.php before calling JS code.

5/5: php: Realtime (4/5 is the aggregated feed which I don't need now)

// Listening to realtime updates is only available in JS
// You can pass the feed token as follows
// Generating tokens for client side usage
$token = **$user1**->getToken();
// Javascript client side feed initialization
// ericFeed = client.feed('user:eric', '{{ token }}');

NOTEs: I replaced $user1 with $eric. This part of the code is put inside eric.php, however, it is not clear what I need to do with

   ericFeed = client.feed('user:eric', '{{ token }}');

which is commented out in step 5/5/ of the tutorial.

There is no more code given in the tutorial at this point.

1

There are 1 answers

1
user6358161 On BEST ANSWER

SOLVED: I report the working code for eric.php and jessica.php

eric.php (the one who creates the feed)

<?php
 //Stream: create feed
 require_once '../../../vendor/autoload.php';
 $client = new GetStream\Stream\Client('MY_KEY', 'MY_SECRET');

 // For the feed group 'user' and user id 'eric' get the feed
 $ericFeed = $client->feed('user', 'eric');

 // Add the activity to the feed
 $data = [
   "actor"=>"eric",
   "verb"=>"like",
   "object"=>"3",
   "tweet"=>"Hello world"
 ];
 $ericFeed->addActivity($data);
 //End stream: create feed
?>

jessica.php: (the one who receives the feed)

 <?php
   require_once '../../../vendor/autoload.php';
   $client = new GetStream\Stream\Client('MY_KEY', 'MY_SECRET');

   //Stream
   $ericFeed = $client->feed('user', 'eric');
   $token = $ericFeed->getToken();
   //Get stream 
 ?>


    <script type="text/javascript" src="path/to/getstream.js"></script> 
    <script type="text/javascript"> 

        var client = stream.connect('MY_KEY', null, 'APP_ID'); 
        var ericFeed = client.feed("user", "eric", "<?php echo $token?>"); 

        function callback(data) { 
        alert("New Feed Received!"); 
        console.log("New Feed received!!!", data); 
        } 

        function successCallback(data) { 
        console.log('now listening to changes in realtime'); 
        } 

        function failCallback(data) { 
        alert('something went wrong, check the console logs'); 
        console.log(data); 
        } 

        ericFeed.subscribe(callback).then(successCallback, failCallback); 
    </script>