Constant stream - Per User?(not sure how to correctly title)

php

#1

Hello,

So heres my issue… or what I do not understand.
My initial thought was that twitters streaming api would make a post request to the url of my choice, in which i can handle data how ever needed… now after testing and finally playing, this isnt the case…

So, my app needs to get the latest follower/unfollowers and save them to the database. Now… this works, but only when i directly run the URL i need to.

Heres an example of my code:

 set_time_limit(60);
    $GLOBALS['time_start'] = time();
    $account = TwitterAccounts::find(8);
    $this->twitter->updateToken($account->token, $account->refresh_token);
    $this->twitter->getApi()->setStreamingCallback(function ($message)
    {
        if ($message !== null) {

            if(isset($message->event))
            {
                switch ($message->event)
                {
                    case "follow":

                        //dd($message);
                        FollowList::create([
                            'account_id' => $message->target->id,
                            'followed_by_account_id' => $message->source->id,
                        ]);
                        echo "{$message->target->screen_name} followed {$message->source->screen_name} <br>";
                       // return true;
                        return false;
                        break;
                    case "unfollow":
                        //dd($message);
                        echo "{$message->source->screen_name} unfollowed {$message->target->screen_name} <br>";
                        return false;
                        break;
                    case "user_update":
                        //When the user update their information
                        break;
                }
            }
            
            flush();
        }
        if (time() - $GLOBALS['time_start'] >= 60) {
            return true;
        }

        return false;
    });

    return $this->twitter->getApi()->user();

Sorry this is hard to explain, but… now, when a user is using the app, if their account receives a new follow or they follow a user, it would be automatically added to the database… but this only works if the url is being run, so how do i handle this?

EDIT:
If i were to remove the parts that close the stream, how would the stream start originally. Would i Cronjob it?
If i were to use the one stream like i have here, I dont understand how the token and stuff will differ from user to user

Thanks.


#2

Bump… ^^