Welcome to pusher_rs - The long-missing Rust Pusher crate.
This library is still under heavy development. Currently in use by one of the projects I am working on.
Open-sourcing it since it has matured to a good level.
A Rust client library for interacting with the Pusher Channels API. This library provides a simple and efficient way to integrate Pusher's real-time functionality into your Rust applications.
Features
- Connect to Pusher Channels
- Subscribe to public, private, presence, and private encrypted channels
- Publish events to channels
- Handle incoming events
- Automatic reconnection with exponential backoff
- Environment-based configuration
- Flexible channel management
- Support for Batch Triggers
Todo
- Improve error handling and logging
- Add comprehensive test suite
- Implement WebHooks support
- Optimize performance for high-load scenarios
- Create more detailed documentation and examples
Installation
Add this to your Cargo.toml:
[]
= "0.1.2"
If you want to use a specific branch or commit, you can specify it like this:
[]
= { = "https://github.com/username/pusher-rs.git", = "main" }
# or
= { = "https://github.com/username/pusher-rs.git", = "commit_hash" }
Configuration
The library uses environment variables for configuration. Create a .env file in your project root with the following variables:
PUSHER_APP_ID=your_app_id
PUSHER_KEY=your_app_key
PUSHER_SECRET=your_app_secret
PUSHER_CLUSTER=your_cluster
PUSHER_USE_TLS=true - this will enforce TLS
Usage
Initializing the client
use ;
async
Connecting
Connect to Pusher:
client.connect.await?;
Subscribing to Channels
Subscribe to a public channel:
client.subscribe.await?;
Subscribe to a private channel:
client.subscribe.await?;
Subscribe to a presence channel:
client.subscribe.await?;
Unsubscribing from Channels
client.unsubscribe.await?;
Binding to Events
Bind to a specific event on a channel:
use Event;
client.bind.await?;
Subscribing to a channel
client.subscribe.await?;
Publishing an event
client.trigger.await?;
Publishing batch events
use BatchEvent;
let batch_events = vec!;
client.trigger_batch.await?;
Handling events
client.bind.await?;
Working with encrypted channels
// Subscribe to an encrypted channel
client.subscribe_encrypted.await?;
// Publish to an encrypted channel
client.trigger_encrypted.await?;
Channel Types
The library supports four types of channels:
- Public
- Private
- Presence
- Private Encrypted
Each channel type has specific features and authentication requirements.
Handling Connection State
Get the current connection state:
let state = client.get_connection_state.await;
println!;
Error Handling
The library uses a custom PusherError type for error handling. You can match on different error variants to handle specific error cases:
use PusherError;
match client.connect.await
Disconnecting
When you're done, disconnect from Pusher:
client.disconnect.await?;
Advanced Usage
Custom Configuration
While the library defaults to using environment variables, you can also create a custom configuration:
use PusherConfig;
use Duration;
let config = PusherConfig ;
Channel Management
The library provides a ChannelList struct for managing subscribed channels:
let mut channel_list = new;
let channel = new;
channel_list.add;
if let Some = channel_list.get
Presence Channels
When subscribing to a presence channel, you can provide user information:
use json;
let channel = "presence-my-channel";
let socket_id = client.get_socket_id.await?;
let user_id = "user_123";
let user_info = json!;
let auth = client.authenticate_presence_channel?;
client.subscribe_with_auth.await?;
Tests
Integration tests live under tests/integration_tests
Just run cargo test --test integration_tests -- --nocapture to start.
More tests are being added. This section will be updated accordingly.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. Here's how you can contribute:
- Fork the repository
- Create your feature branch (
git checkout -b feat/amazing-feature) - Commit your changes (
git commit -m 'feat: Add some amazing feature') - Push to the branch (
git push origin feat/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License.