- How do I set up OAuth credentials for Twitter’s API in this Laravel package?
- The package abstracts OAuth 1.0a authentication. Configure your Twitter API keys and secrets in Laravel’s `.env` file (e.g., `TWITTER_CONSUMER_KEY`, `TWITTER_CONSUMER_SECRET`), then pass them to the package’s stream client. No manual OAuth flow is needed—it handles token requests automatically for Filtered or User Streams.
- Can I use this package to track specific keywords or hashtags in real-time?
- Yes, the package supports Twitter’s Filtered Stream API, which lets you specify keywords, hashtags, or user IDs. Pass an array of tracking rules (e.g., `['#laravel', 'php']`) to the stream client’s `filter()` method to start a live feed matching those terms.
- What Laravel versions does this package support, and how do I install it?
- The package targets Laravel 8.x and 9.x (PHP 8.0+). Install via Composer: `composer require redwebcreation/twitter-stream-api`. No additional Laravel service providers are needed—just configure your Twitter credentials and start streaming in a controller or job.
- How do I handle incoming tweets or events from the stream in my Laravel app?
- Use the package’s `onTweet()` or `onEvent()` callback methods to process raw JSON data. For example, bind a closure to `onTweet(fn($tweet) => Log::info($tweet['text']))` to log tweets. For complex logic, dispatch Laravel events or queue jobs (e.g., `ProcessTweetJob::dispatch($tweet)`).
- Does this package support Twitter API v2’s new endpoints like the Academic Research API?
- No, this package only supports Twitter API v1.1’s Filtered Stream and User Stream endpoints. For v2 features (e.g., Academic Research API or Premium endpoints), you’ll need a separate library like `abraham/twitteroauth` or Twitter’s official v2 SDK, as the APIs use different auth and response formats.
- How do I handle rate limits or connection drops in a production stream?
- Twitter’s API enforces rate limits (e.g., 500–1,500 requests/15 minutes for streams). The package doesn’t include retry logic, so implement exponential backoff using Guzzle middleware or Laravel’s `retry` helper. For connection drops, wrap the stream in a loop with error handling (e.g., `try-catch` blocks) or use a circuit breaker like Spatie’s package.
- Can I store tweets in a database or Elasticsearch using this package?
- The package returns raw tweet JSON, so you’ll need to parse and normalize it before storage. For databases, use Laravel’s Eloquent or a JSONB column (PostgreSQL). For Elasticsearch, pipe tweets to a queue and process them with a job that indexes data via `elasticsearch/elasticsearch`. Consider deduplicating retweets or likes to avoid data bloat.
- Is this package suitable for high-volume streams (e.g., thousands of tweets per minute)?
- For high-volume streams, offload processing to Laravel queues (e.g., Redis or database queues) and use Horizon for worker scaling. Avoid blocking HTTP requests—start the stream in a long-running job or daemon (e.g., Laravel Forge supervisor). Monitor memory usage, as raw tweet processing can be resource-intensive.
- What are the alternatives to this package for Laravel Twitter streaming?
- Alternatives include `abraham/twitteroauth` (low-level, requires manual stream handling) or Twitter’s official PHP SDK (`twitter/api-php`). For Laravel-specific solutions, consider `spatie/laravel-twitter` (limited to v1.1) or building a custom wrapper around Guzzle for v2. This package stands out for its Laravel integration and callback simplicity.
- How do I test my Twitter stream in development without hitting rate limits?
- Use Twitter’s sandbox environment (if available) or mock the stream responses in tests with Laravel’s HTTP client. For unit tests, inject a fake stream client (e.g., via dependency injection) and assert callback invocations. For integration tests, use a lightweight test database to store processed tweets and verify workflows without real API calls.