faye-redis-ng
A Redis-based backend engine for Faye messaging server, enabling distribution across multiple web servers.
Features
- 🚀 Scalable: Distribute Faye across multiple server instances
- 🔄 Real-time synchronization: Messages are routed between servers via Redis
- 💪 Reliable: Built-in connection pooling and retry mechanisms
- 🔒 Secure: Support for Redis authentication and SSL/TLS
- 📊 Observable: Comprehensive logging and error handling
Installation
Add this line to your application's Gemfile:
gem 'faye-redis-ng'
And then execute:
bundle install
Or install it yourself:
gem install faye-redis-ng
Usage
Basic Setup
require 'faye'
require 'faye-redis-ng'
# Create a Faye server with Redis backend
bayeux = Faye::RackAdapter.new(app, {
mount: '/faye',
timeout: 25,
engine: {
type: Faye::Redis,
host: 'localhost',
port: 6379,
database: 0
}
})
Configuration Options
{
# Redis connection
host: 'localhost', # Redis server host
port: 6379, # Redis server port
database: 0, # Redis database number
password: nil, # Redis password (optional)
# Connection pool
pool_size: 5, # Connection pool size
pool_timeout: 5, # Pool checkout timeout (seconds)
# Timeouts
connect_timeout: 1, # Connection timeout (seconds)
read_timeout: 1, # Read timeout (seconds)
write_timeout: 1, # Write timeout (seconds)
# Retry configuration
max_retries: 3, # Max retry attempts
retry_delay: 1, # Initial retry delay (seconds)
# Data expiration
client_timeout: 60, # Client session timeout (seconds)
message_ttl: 3600, # Message TTL (seconds)
# Logging
log_level: :info, # Log level (:silent, :info, :debug)
# Namespace
namespace: 'faye' # Redis key namespace
}
Advanced Configuration
With Authentication
engine: {
type: Faye::Redis,
host: 'redis.example.com',
port: 6379,
password: 'your-redis-password'
}
With SSL/TLS
engine: {
type: Faye::Redis,
host: 'redis.example.com',
port: 6380,
ssl: {
enabled: true,
cert_file: '/path/to/cert.pem',
key_file: '/path/to/key.pem',
ca_file: '/path/to/ca.pem'
}
}
Custom Namespace
engine: {
type: Faye::Redis,
host: 'localhost',
port: 6379,
namespace: 'my-app' # All Redis keys will be prefixed with 'my-app:'
}
Multi-Server Setup
To run Faye across multiple servers, simply configure each server with the same Redis backend:
Server 1 (config.ru)
require 'faye'
require 'faye-redis-ng'
bayeux = Faye::RackAdapter.new(app, {
mount: '/faye',
timeout: 25,
engine: {
type: Faye::Redis,
host: 'redis.example.com',
port: 6379
}
})
run bayeux
Server 2 (config.ru)
# Same configuration as Server 1
require 'faye'
require 'faye-redis-ng'
bayeux = Faye::RackAdapter.new(app, {
mount: '/faye',
timeout: 25,
engine: {
type: Faye::Redis,
host: 'redis.example.com', # Same Redis server
port: 6379
}
})
run bayeux
Now clients can connect to either server and messages will be routed correctly between them!
Architecture
faye-redis-ng uses the following Redis data structures:
- Client Registry: Hash and Set for tracking active clients
- Subscriptions: Sets for managing channel subscriptions
- Message Queue: Lists for queuing messages per client
- Pub/Sub: Redis Pub/Sub for cross-server message routing
Key Components
- Connection Manager: Handles Redis connection pooling and retries
- Client Registry: Manages client lifecycle and sessions
- Subscription Manager: Handles channel subscriptions with wildcard support
- Message Queue: Manages message queuing and delivery
- Pub/Sub Coordinator: Routes messages between server instances
Development
Running Tests
bundle exec rspec
Building the Gem
gem build faye-redis-ng.gemspec
Installing Locally
gem install ./faye-redis-ng-0.1.0.gem
Releasing to RubyGems
This project uses GitHub Actions for automated releases. To publish a new version:
- Update the version in
lib/faye/redis/version.rb
- Commit the version change
- Create and push a git tag:
git tag v0.1.0
git push origin v0.1.0
The CI/CD pipeline will automatically:
- Run all tests across multiple Ruby versions
- Build the gem
- Publish to RubyGems (requires
RUBYGEMS_API_KEY
secret) - Create a GitHub release with the gem attached
Prerequisites:
- Add
RUBYGEMS_API_KEY
to GitHub repository secrets - The tag must start with 'v' (e.g., v0.1.0, v1.2.3)
Troubleshooting
Connection Issues
If you're experiencing connection issues:
- Verify Redis is running:
redis-cli ping
- Check Redis connection settings
- Ensure firewall allows Redis port (default 6379)
- Check logs for detailed error messages
Message Delivery Issues
If messages aren't being delivered:
- Verify all servers use the same Redis instance
- Check that clients are subscribed to the correct channels
- Ensure Redis pub/sub is working:
redis-cli PUBSUB CHANNELS
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
MIT License - see LICENSE file for details
Acknowledgments
- Built for the Faye messaging system
- Inspired by the original faye-redis gem