Pusher HTTP Rust Client
A Rust client for interacting with the Pusher HTTP API, allowing you to publish events, authorize channels, authenticate users, and handle webhooks from your Rust applications.
Features
- Trigger events on public, private, and presence channels
- Trigger events to specific users (User Authentication)
- Trigger batch events for efficiency
- Support for end-to-end encrypted channels
- Authorize client subscriptions to private, presence, and encrypted channels
- Authenticate users for user-specific Pusher features
- Terminate user connections
- Validate and process incoming Pusher webhooks
- Configurable host, port, scheme (HTTP/HTTPS), and timeout
- Asynchronous API using
async/await
- Typed responses and errors
Installation
Add the following to your Cargo.toml
:
[]
# If publishing to crates.io:
# pusher-http-rust = "0.1.0" # Replace with the desired version
# Or, for local development:
# pusher-http-rust = { path = "../path/to/pusher-http-rust" }
= "1.0"
= { = "1", = ["full"] }
# reqwest is used internally but you might need it for response handling
# reqwest = { version = "0.11", features = ["json"] }
Then run:
Usage
1. Initialization
Configure and create a Pusher
client:
use ;
async
You can also initialize from a Pusher URL:
let pusher_from_url = from_url?;
2. Triggering Events
use json;
let channels = vec!;
let event_name = "new-message";
let data = json!;
match pusher.trigger.await
Encrypted channels
If channels
contains a single encrypted channel (e.g. "private-encrypted-mychannel"
) and you’ve set the encryption_master_key
, the library will encrypt data
automatically.
Excluding a recipient
use TriggerParams;
let params = TriggerParams ;
pusher
.trigger
.await?;
3. Triggering Batch Events
use BatchEvent;
use json;
let batch = vec!;
match pusher.trigger_batch.await
4. Authorizing Channels
Typically done in your HTTP handler when a client attempts to subscribe:
use json;
// Example values from client
let socket_id = "123.456";
let channel_name = "private-mychannel";
// For presence channels, include user data:
let presence_data = Some;
match pusher.authorize_channel
5. Authenticating Users
For server-to-user events:
use json;
// Example values from client
let socket_id = "789.012";
let user_data = json!;
match pusher.authenticate_user
6. Sending an Event to a User
let user_id = "user-bob";
let event_name = "personal-notification";
let data = json!;
match pusher.send_to_user.await
7. Terminating User Connections
let user_id = "user-charlie";
match pusher.terminate_user_connections.await
8. Handling Webhooks
use BTreeMap;
// Incoming request data (example)
let mut headers = new;
headers.insert;
headers.insert;
let body = r#"{
"time_ms": 1600000000000,
"events":[{"name":"channel_occupied","channel":"my-channel"}]
}"#;
let webhook = pusher.webhook;
if webhook.is_valid else
9. Example: Integration with Axum
use ;
use ;
use ;
use ;
use ;
async
async
async
Configuration Options
Config
methods:
new(app_id, key, secret)
— basic initializationcluster(name)
— set cluster (e.g."eu"
)use_tls(bool)
— enable HTTPS (defaulttrue
)port(number)
— custom porttimeout(Duration)
— HTTP request timeoutencryption_master_key_base64(key)
— 32-byte base64 key for encrypted channels
Error Handling
All fallible methods return Result<T, PusherError>
.
PusherError
variants:
Request
— HTTP request errorsWebhook
— webhook processing errorsConfig
— invalid configurationValidation
— input validation errorsEncryption
— encryption/decryption errorsJson
—serde_json
errorsHttp
—reqwest
errors
Contributing
Contributions are welcome! Please open issues or pull requests.
For major changes, please discuss via issue first.
License
This project is licensed under the GNU Affero General Public License v3.0 License. See LICENSE.md
for details.