stevegraham/slanger
GitHub: stevegraham/slanger
Slanger 是一个用 Ruby 实现的 Pusher 协议兼容服务器,让开发者能够在自有基础设施上部署与 Pusher 客户端库完全兼容的实时消息服务。
Stars: 1690 | Forks: 201
# Slanger
## 不再维护 - Slanger 已被弃用且作者不再维护。请自行承担使用风险。
[](http://badge.fury.io/rb/slanger) [](https://travis-ci.org/stevegraham/slanger)
**重要提示!Slanger 不应该被包含在您的 Gemfile 中。RubyGems 仅用作分发机制。如果您将其包含在您的应用中,可能会导致依赖冲突。针对应用兼容性更新依赖的 PR 将被关闭。感谢您的阅读并尽情使用 Slanger!**
## 典型用法
```
gem install slanger
redis-server &> /dev/null &
slanger --app_key 765ec374ae0a69f4ce44 --secret your-pusher-secret
```
Slanger 是 Pusher 协议的独立服务器 Ruby 实现。它
并非设计用于在 Rails 或 sinatra 应用内部运行,但可以很方便地
作为 gem 进行安装。
Bundler 有多种用途,其中之一对安装非常有用。
## 关于
Slanger 是一个用 Ruby 编写的 Pusher 协议开源服务器实现。
它旨在跨 N 个节点进行水平扩展,并且与订阅者连接到哪个 Slanger 节点无关,
即订阅同一 channel 的订阅者不需要连接到同一个 Slanger 节点。
多个 Slanger 节点无需特殊配置即可位于负载均衡器之后。
本质上,它的设计非常易于扩展。
Presence channel 的状态通过 Redis 共享。当第一个订阅者连接时,
channel 会在给定的 Slanger 节点内部延迟实例化。当
presence channel 在 Slanger 节点内实例化时,它会向 Redis 查询
该 channel 在系统内所有节点的全局状态,然后
在内部复制该状态。此后,当订阅者连接或
断开连接时,该节点会向所有感兴趣的节点(即
至少有一个订阅者对该 channel 感兴趣的所有节点)发布 presence 消息。
Slanger 足够智能,能够判断新的 channel 订阅是否属于
同一用户。在这种情况下,它不会向订阅者发送 presence 消息。
例如,当用户打开多个浏览器标签页时就会发生这种情况。
以一个由 presence channel 支持的聊天室为例,人们不希望
看到 "Barrington" 在 presence 名单中出现 N 次,仅仅因为 Barrington
在 N 个浏览器标签页中打开了该聊天室。
Slanger 的设计具有高可用性和分区容错性,
并保证最终一致性,这在实践中是瞬间完成的。
# 如何使用
## 要求
- Ruby 2.1.2 或更高版本
- Redis
## 服务器设置
大多数 Linux 发行版默认的打开文件数限制非常低。为了维持超过 1024 个(默认)连接,您需要对系统应用以下更改:
添加到 `/etc/sysctl.conf`:
```
fs.file-max = 50000
```
添加到 `/etc/security/limits.conf`:
```
* hard nofile 50000
* soft nofile 50000
* hard nproc 50000
* soft nproc 50000
```
## 使用 Haproxy 进行集群负载均衡设置
如果您想在集群中运行多个 slanger 实例,一种选择是使用 Haproxy 来平衡连接。
一个基本的配置可以在 `examples` 文件夹中找到。
Haproxy 也可以用于 SSL 终止,让 slanger 不必处理 SSL 检查等事务,从而使其更轻量。
## 启动服务
Slanger被打包为一个 Rubygem。安装该 gem 后,'slanger' 可执行文件就可用了。`slanger` 可执行文件接受参数,其中两个是强制性的:`--app_key` 和 `--secret`。这些可以与您用于 Pusher 的凭据相同,但并非必须。之所以需要它们,是因为 Slanger 执行与 Pusher 相同的 API 请求 HMAC 签名。
__重要提示:__ Redis 必须在 Slanger 期望的位置运行(要么在 localhost:6379,要么在您通过选项标志告诉 Slanger 的其他位置),否则 Slanger 将静默失败。我还没弄清楚如何让 em-hiredis 将不可达的主机视为错误
```
$ gem install slanger
$ redis-server &> /dev/null &
$ slanger --app_key 765ec374ae0a69f4ce44 --secret your-pusher-secret
```
如果一切按计划进行,您应该会在 STDOUT 看到以下输出
```
.d8888b. 888
d88P Y88b 888
Y88b. 888
"Y888b. 888 8888b. 88888b. .d88b. .d88b. 888d888
"Y88b. 888 "88b 888 "88b d88P"88b d8P Y8b 888P"
"888 888 .d888888 888 888 888 888 88888888 888
Y88b d88P 888 888 888 888 888 Y88b 888 Y8b. 888
"Y8888P" 888 "Y888888 888 888 "Y88888 "Y8888 888
888
Y8b d88P
"Y88P"
Slanger API server listening on port 4567
Slanger WebSocket server listening on port 8080
```
## Ubuntu upstart 脚本
如果您使用的是 Ubuntu,您可能会发现这个启动脚本非常有用。下面的步骤将创建一个 init 脚本,使 slanger 在启动时运行并在失败时重启。
打开 `/etc/init/slanger` 并添加:
```
start on started networking and runlevel [2345]
stop on runlevel [016]
respawn
script
LANG=en_US.UTF-8 /usr/local/rvm/gems/ruby-RUBY_VERISON/wrappers/slanger --app_key KEY --secret SECRET --redis_address redis://REDIS_IP:REDIS_PORT/REDIS_DB
end script
```
此示例假设您使用的是 rvm 和自定义的 redis 配置
然后,要启动 / 停止服务,只需执行
```
service slanger start
service slanger stop
```
## 修改应用程序代码以使用 Slanger 服务
一旦您有一个 Slanger 实例监听传入连接,您就需要修改应用程序代码以使用 Slanger endpoint 而不是 Pusher。幸运的是,这非常简单、无侵入性、易于逆转,而且非常轻松。
首先,您需要向发布事件到 Pusher HTTP REST API 的服务器端组件添加代码,通常这意味着告诉 Pusher 客户端使用不同的主机和端口,例如,考虑这个 Ruby 示例
```
...
Pusher.host = 'slanger.example.com'
Pusher.port = 4567
```
您还需要在客户端 JavaScript 中对 Pusher JavaScript 客户端执行相同的操作,例如
```
```
当然,您可以将所有发往 `ws.example.com` 的请求代理到 Slanger 节点的 8080 端口,将发往 `api.example.com` 的请求代理到 Slanger 节点的 4567 端口,这样您只需要设置 Pusher 客户端的 host 属性即可。
# 配置选项
Slanger 支持多个配置选项,可以在调用时作为命令行参数提供。您也可以提供一个包含配置选项的 yaml 文件。如果您结合使用配置文件和其他配置选项,命令行传递的值将优先。这使得运行仅有少量差异的多个实例变得容易。
```
-k or --app_key This is the Pusher app key you want to use. This is a required argument on command line or in optional config file
-s or --secret This is your Pusher secret. This is a required argument on command line or in optional config file
-C or --config_file Path to Yaml file that can contain all or some of the configuration options, including required arguments
-r or --redis_address An address where there is a Redis server running. This is an optional argument and defaults to redis://127.0.0.1:6379/0
-a or --api_host This is the address that Slanger will bind the HTTP REST API part of the service to. This is an optional argument and defaults to 0.0.0.0:4567
-w or --websocket_host This is the address that Slanger will bind the WebSocket part of the service to. This is an optional argument and defaults to 0.0.0.0:8080
-i or --require Require an additional file before starting Slanger to tune it to your needs. This is an optional argument
-p or --private_key_file Private key file for SSL support. This argument is optional, if given, SSL will be enabled
-b or --webhook_url URL for webhooks. This argument is optional, if given webhook callbacks will be made http://pusher.com/docs/webhooks
-c or --cert_file Certificate file for SSL support. This argument is optional, if given, SSL will be enabled
-v or --[no-]verbose This makes Slanger run verbosely, meaning WebSocket frames will be echoed to STDOUT. Useful for debugging
--pid_file The path to a file you want slanger to write it's PID to. Optional.
```
# 为什么使用 Slanger 而不是 Pusher?
您可能想要使用 Slanger 而不是 Pusher 的原因有以下几个,例如:
- 您处于严格监管的行业,担心将数据发送给第三方,并且拥有自己的基础设施是组织要求。
- 您可能像我一样在没有互联网连接的飞机上旅行。飞机旅行是完成大量工作的绝佳时机,不幸的是外部服务通常也无法访问。通过在开发环境中运行本地 Slanger 实例并在生产环境中使用 Pusher,可以消除互联网连接作为开发环境的依赖。
- 从您的测试套件中移除网络依赖。
- 您想扩展 Pusher 协议或有某些特殊要求。如果这种情况适用于您,您可能就不走运了,因为 Pusher 不太可能实现某些东西来适应您的特殊用例,而且 rightly so。使用 Slanger,您可以自由地以任何适合您目的的方式修改和扩展其行为。
# 为什么我要编写 Slanger
我想编写一个非平凡的 evented 应用程序。我还想写一本关于 Ruby 中 evented 编程的书,因为我觉得关于这个主题的优质信息很少,而这个项目很适合展示给出版商看。
Pusher 是一项很棒的服务,价格非常合理,由一群很棒的团队运营。在您的下一个项目中试试他们吧。
# 作者
- Stevie Graham
# 核心团队
- Stevie Graham
- Mark Burns
# 贡献者
- Stevie Graham
- Mark Burns
- Florian Gilcher
- Claudio Poli
© 2011 a Stevie Graham joint.
标签:Deprecated, EventMachine, Gem, Presence Channel, Pub/Sub, Pusher协议, Redis, Ruby, Slanger, WebSocket, 依赖分析, 即时通讯, 发布订阅, 实时处理, 实时消息推送, 实时通信, 开源实现, 搜索引擎查询, 服务端, 未维护, 消息代理, 知识库, 网络编程, 负载均衡, 跨平台兼容