https://github.com/anycable/anycable-rack-server
AnyCable-compatible Ruby Rack middleware
https://github.com/anycable/anycable-rack-server
anycable rack ruby
Last synced: 7 months ago
JSON representation
AnyCable-compatible Ruby Rack middleware
- Host: GitHub
- URL: https://github.com/anycable/anycable-rack-server
- Owner: anycable
- License: mit
- Created: 2018-12-01T20:20:26.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-06-30T01:37:45.000Z (over 2 years ago)
- Last Synced: 2025-04-04T10:37:07.360Z (8 months ago)
- Topics: anycable, rack, ruby
- Language: Ruby
- Size: 125 KB
- Stars: 26
- Watchers: 4
- Forks: 14
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://cultofmartians.com/tasks/anycable-ruby-server.html)
[](https://rubygems.org/gems/anycable-rack-server)
[](https://github.com/anycable/anycable-rack-server/actions)
# anycable-rack-server
[AnyCable](https://anycable.io)-compatible Rack hijack based Ruby Web Socket server designed for development and testing purposes.
## Using with Rack
```ruby
# Initialize server instance first.
ws_server = AnyCable::Rack::Server.new
app = Rack::Builder.new do
map "/cable" do
run ws_server
end
end
# NOTE: don't forget to call `start!` method
ws_server.start!
run app
```
## Usage with Rails
Add `gem "anycable-rack-server"` to you `Gemfile` and make sure your Action Cable adapter is set to `:any_cable`. That's it! We automatically start AnyCable Rack server for you at `/cable` path.
## Configuration
AnyCable Rack Server uses [`anyway_config`](https://github.com/palkan/anyway_config) gem for configuration; thus it is possible to set configuration parameters through environment vars (prefixed with `ANYCABLE_`), `config/anycable.yml` file or `secrets.yml` when using Rails.
**NOTE:** AnyCable Rack Server uses the same config name (i.e., env prefix, YML file name, etc.) as AnyCable itself.
You can pass a config object as the option to `AnyCable::Rack::Server.new`:
```ruby
server = AnyCable::Server::Rack.new(config: AnyCable::Rack::Config.new(**params))
```
If no config is passed, a default, global, configuration would be used (`AnyCable::Rack.config`).
When using Rails, `config.anycable_rack` points to `AnyCable::Rack.config`.
### Headers
You can customize the headers being sent with each gRPC request.
Default headers: `'cookie', 'x-api-token'`.
Can be specified via configuration:
```ruby
AnyCable::Rack.config.headers = ["cookie", "x-my-header"]
```
Or in Rails:
```ruby
# .rb
config.any_cable_rack.headers = %w[cookie]
```
### Rails-specific options
```ruby
# Mount WebSocket server at the specified path
config.any_cable_rack.mount_path = "/cable"
# NOTE: here we specify only the port (we assume that a server is running locally)
config.any_cable_rack.rpc_port = 50051
```
## Broadcast adapters
AnyCable Rack supports Redis (default) and HTTP broadcast adapters
(see [the documentation](https://docs.anycable.io/ruby/broadcast_adapters)).
Broadcast adapter is inherited from AnyCable configuration (so, you don't need to configure it twice).
### Using HTTP broadcast adapter
### With Rack
```ruby
AnyCable::Rack.config.broadast_adapter = :http
ws_server = AnyCable::Rack::Server
app = Rack::Builder.new do
map "/cable" do
run ws_server
end
map "/_anycable_rack_broadcast" do
run ws_server.broadcast
end
end
```
### With Rails
By default, we mount broadcasts endpoint at `/_anycable_rack_broadcast`.
You can change this setting:
```ruby
config.any_cable_rack.http_broadcast_path = "/_my_broadcast"
```
**NOTE:** Don't forget to configure `http_broadcast_url` for AnyCable pointing to your web server and the specified broadcast path.
## Testing
Run units with `bundle exec rake`.
## Contributing
Bug reports and pull requests are welcome on GitHub at [https://github.com/anycable/anycable-rack-server](https://github.com/anycable/anycable-rack-server).
## License
The gem is available as open source under the terms of the [MIT License](./LICENSE).