- How do I install spatie/twitter-labs in a Laravel project?
- Run `composer require spatie/twitter-labs` to install the package. Ensure your project uses PHP 7.2+ and has ReactPHP installed (it’s a dependency). No additional Laravel-specific setup is required beyond including the namespace in your code.
- Does this package support Laravel’s queue system for processing tweets?
- Yes, you can dispatch Laravel queue jobs inside the stream callback to process tweets asynchronously. For example, use `Bus::dispatch(new ProcessTweet($tweet))` within the stream handler to offload work to a worker.
- What Laravel versions does spatie/twitter-labs support?
- The package is framework-agnostic but works with Laravel 6+ (PHP 7.2+). For Laravel 5.x, you’ll need to manually handle ReactPHP’s event loop integration, as newer Laravel versions align better with async patterns.
- How do I handle errors if the Twitter Labs stream disconnects?
- ReactPHP’s event loop provides reconnection logic, but you should implement custom retry logic in your callback. For example, log failures and use Laravel’s queue retries or a cron job to resume the stream.
- Can I use this package without knowing ReactPHP?
- Yes, the package abstracts ReactPHP’s event loop, so you can use it like a synchronous client. However, for advanced use cases (e.g., long-running streams), understanding ReactPHP’s event loop will help optimize performance and error handling.
- What’s the difference between this and Spatie’s older twitter-streaming-api?
- This package targets Twitter’s **Filtered Stream API** (Labs), while the older package used deprecated endpoints. Migration is straightforward: replace the old client with `TwitterLabs` and update your stream rules to match the new API format.
- How do I test this package in a CI environment?
- Mock the Twitter Labs API responses using ReactPHP’s `MockHttp` or a library like `vcr`. For unit tests, inject a fake event loop and verify callbacks are triggered with expected data. Integration tests may require a local ReactPHP server.
- Is this package suitable for production use?
- Use it cautiously—Twitter Labs endpoints are experimental and may change or deprecate without notice. For production, consider Twitter API v2 (e.g., `tweetings/laravel-twitter-api`) as a fallback or primary solution.
- How do I persist tweets from the stream to a database?
- The package doesn’t include persistence logic. Use Eloquent models or a queue job to store tweets in your database. For example, create a `Tweet` model and save each tweet in the stream callback: `Tweet::create($tweetData)`.
- Are there alternatives if Twitter Labs is deprecated?
- Yes, consider `abraham/twitteroauth` or `tweetings/laravel-twitter-api` for stable Twitter API v2 access. These packages support polling or webhooks for real-time data, though they lack the async streaming model of this package.