PHPackages                             spatie/laravel-twitter-streaming-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. spatie/laravel-twitter-streaming-api

ActiveLibrary[API Development](/categories/api)

spatie/laravel-twitter-streaming-api
====================================

Easily work with the Twitter Streaming API in a Laravel app

2.2.2(4mo ago)20846.3k—10%234MITPHPPHP ^8.0CI passing

Since Jan 13Pushed 4mo ago4 watchersCompare

[ Source](https://github.com/spatie/laravel-twitter-streaming-api)[ Packagist](https://packagist.org/packages/spatie/laravel-twitter-streaming-api)[ Docs](https://github.com/spatie/laravel-twitter-streaming-api)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-laravel-twitter-streaming-api/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (10)Dependencies (5)Versions (19)Used By (4)

Easily work with the Twitter Streaming API in a Laravel app
===========================================================

[](#easily-work-with-the-twitter-streaming-api-in-a-laravel-app)

[![Latest Version on Packagist](https://camo.githubusercontent.com/10b248f22cee13a404514bfbca744309cf9c3354d0ca9a6886f5deb465e4e38e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d747769747465722d73747265616d696e672d6170692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-twitter-streaming-api)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/325ff378300a301388a37bbecb6eb7ee209e052e0f4af61b8c65b6a302f8a16e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d747769747465722d73747265616d696e672d6170692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-twitter-streaming-api)

Twitter provides a streaming API with which you can do interesting things such as listening for tweets that contain specific strings or actions a user might take (e.g. liking a tweet, following someone,...). This package makes it very easy to work with the API.

```
TwitterStreamingApi::publicStream()
->whenHears('#laravel', function(array $tweet) {
    echo "{$tweet['user']['screen_name']} tweeted {$tweet['text']}";
})
->startListening();
```

Here's [an example Laravel application](https://github.com/spatie/laravel-twitter-streaming-api-example-app) with the package pre-installed. It contains [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/master/app/Console/Commands/ListenForHashTags.php)to kick off the listening process.

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/aa48f1cac89b9fc40e12c03b6e184456648270a8eb7d51a36c8d8485847a3089/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d747769747465722d73747265616d696e672d6170692e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-twitter-streaming-api)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-twitter-streaming-api
```

The config file must be published with this command:

```
php artisan vendor:publish --provider="Spatie\LaravelTwitterStreamingApi\TwitterStreamingApiServiceProvider" --tag="config"
```

It will be published in `config/laravel-twitter-streaming-api.php`

```
return [

    /*
     * To work with Twitter's Streaming API you'll need some credentials.
     *
     * If you don't have credentials yet, head over to https://developers.twitter.com/
     */

    'handle' => env('TWITTER_HANDLE'),

    'api_key' => env('TWITTER_API_KEY'),

    'api_secret_key' => env('TWITTER_API_SECRET_KEY'),

    'bearer_token' => env('TWITTER_BEARER_TOKEN'),
];
```

Getting credentials
-------------------

[](#getting-credentials)

In order to use this package you'll need to get some credentials from Twitter. Head over to the [Developer Portal on Twitter](https://developers.twitter.com/) to create an application.

Once you've created your application, click on the `Keys and tokens` tab to retrieve your `bearer_token`, `api_key`and `api_secret_key`.

[![Keys and tokens tab on Twitter](docs/tokens.png)](docs/tokens.png)

Usage
-----

[](#usage)

Currently, this package works with the public stream and the user stream. Both the `PublicStream` and `UserStream`classes provide a `startListening` function that kicks of the listening process. Unless you cancel it your PHP process will execute that function forever. No code after the function will be run.

In the example below a facade is used. If you don't like facades you can replace them with

```
app(Spatie\LaravelTwitterStreamingApi\TwitterStreamingApi::class)
```

### The public stream

[](#the-public-stream)

The public stream can be used to listen for specific words that are being tweeted.

The first parameter of `whenHears` must be a string, or an array containing the word or words you want to listen for. The second parameter should be a callable that will be executed when one of your words is used on Twitter.

```
use TwitterStreamingApi;

TwitterStreamingApi::publicStream()
->whenHears('#laravel', function(array $tweet) {
    echo "{$tweet['user']['screen_name']} tweeted {$tweet['text']}";
})
->startListening();
```

### The user stream

[](#the-user-stream)

```
use TwitterStreamingApi;

TwitterStreamingApi::userStream()
->onEvent(function(array $event) {
    if ($event['event'] === 'favorite') {
        echo "Our tweet {$event['target_object']['text']} got favorited by {$event['source']['screen_name']}";
    }
})
->startListening();
```

Suggestion on how to run in a production environment
----------------------------------------------------

[](#suggestion-on-how-to-run-in-a-production-environment)

When using this in production you could opt to create [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/8175995/app/Console/Commands/ListenForHashTags.php)to listen for incoming events from Twitter. You can use [Supervisord](http://supervisord.org/) to make sure that command is running all the time.

A word to the wise
------------------

[](#a-word-to-the-wise)

These APIs work in realtime, so they could report a lot of activity. If you need to do some heavy work processing that activity it's best to put that work in a queue to keep your listening process fast.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

60

—

FairBetter than 98% of packages

Maintenance76

Regular maintenance activity

Popularity46

Moderate usage in the ecosystem

Community30

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 58.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~207 days

Recently: every ~368 days

Total

17

Last Release

132d ago

Major Versions

0.0.2 → 1.0.02017-01-14

1.4.1 → 2.0.02021-05-17

PHP version history (5 changes)0.0.1PHP ^7.0

1.2.0PHP ^7.2

1.4.0PHP ^7.3

1.4.1PHP ^8.0|^7.3

2.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (61 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (13 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (6 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (3 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (2 commits)")[![felixdorn](https://avatars.githubusercontent.com/u/55788595?v=4)](https://github.com/felixdorn "felixdorn (2 commits)")[![RobLui](https://avatars.githubusercontent.com/u/10846766?v=4)](https://github.com/RobLui "RobLui (1 commits)")[![ttomdewit](https://avatars.githubusercontent.com/u/2845400?v=4)](https://github.com/ttomdewit "ttomdewit (1 commits)")[![vool](https://avatars.githubusercontent.com/u/441840?v=4)](https://github.com/vool "vool (1 commits)")[![amiranagram](https://avatars.githubusercontent.com/u/38536188?v=4)](https://github.com/amiranagram "amiranagram (1 commits)")[![fredylg](https://avatars.githubusercontent.com/u/1256524?v=4)](https://github.com/fredylg "fredylg (1 commits)")[![introwit](https://avatars.githubusercontent.com/u/11228182?v=4)](https://github.com/introwit "introwit (1 commits)")[![irazasyed](https://avatars.githubusercontent.com/u/1915268?v=4)](https://github.com/irazasyed "irazasyed (1 commits)")[![matthewtrask](https://avatars.githubusercontent.com/u/4731244?v=4)](https://github.com/matthewtrask "matthewtrask (1 commits)")[![Omranic](https://avatars.githubusercontent.com/u/406705?v=4)](https://github.com/Omranic "Omranic (1 commits)")

---

Tags

apilaravelphpstreamingtweetstwitterspatielaravel-twitter-streaming-api

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/spatie-laravel-twitter-streaming-api/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-laravel-twitter-streaming-api/health.svg)](https://phpackages.com/packages/spatie-laravel-twitter-streaming-api)
```

###  Alternatives

[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.5k30.7M298](/packages/spatie-laravel-query-builder)[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.8k3](/packages/defstudio-telegraph)[spatie/laravel-export

Create a static site bundle from a Laravel app

674146.0k6](/packages/spatie-laravel-export)[spatie/laravel-route-discovery

Auto register routes using PHP attributes

24052.0k3](/packages/spatie-laravel-route-discovery)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5021.9k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
