Slanger是用Ruby编写的Pusher协议的开源服务器实现。
根据Slanger项目README.md,Slanger不适合在Rails中运行。
它不是设计用于在Rails或sinatra应用程序中运行
它可以很好地将它与Ruby on Rails应用程序一起停靠。实施该步骤的步骤是什么?
答案 0 :(得分:2)
您可以在下面找到详细步骤。供您参考,所有代码均可在a GitHub repository上找到。
Slanger depends on Redis。首先,您需要一个Redis容器来运行。
在您的docker-compose.yml中,添加(作为示例):
redis:
image: redis:4.0-alpine
为Slanger
添加Dockerfile(例如,Dockerfile-slanger)FROM ruby:2.3.3
RUN gem install slanger
CMD slanger --app_key $APPKEY --secret $APPSECRET -r redis://$REDIS_HOST:$REDIS_PORT/$REDIS_DB
更新您的docker-compose.yml
slanger:
build:
context: .
dockerfile: Dockerfile-slanger
depends_on:
- redis
ports:
- "8080:8080"
env_file:
- .env.slanger
环境文件.env.slanger看起来像这样:
APPKEY=application_key
APPSECRET=the_secret_for_the_application
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB=Slanger
同样在你的docker-compose.yml中,将slanger添加到你的Rails应用程序的依赖列表中:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
- slanger
env_file:
- .env
现在您需要将推送器gem添加到您的Gemfile
gem 'pusher'
接下来,你需要通过添加文件config / initializers / pusher.rb在你的Rails应用程序中配置Slanger
Pusher.app_id='application_id'
Pusher.key='application_key'
Pusher.secret='the_secret_for_the_application'
Pusher.host='slanger'
Pusher.port=4567
Pusher.logger=Rails.logger
就是这样。现在,您可以使用Pusher in Rails通过Slanger API端口将消息发布到Slanger服务器的通道,还可以使用JavaScript中的Pusher通过WebSocket订阅Slanger服务器的通道。请在下面的示例中找到。
滑轨
class MessageController < ApplicationController
def subscribe
puts 'Subscribed'
end
def publish
message = search_params.require(:message)
Pusher.trigger('MY_CHANNEL', 'AN_EVENT', message: message)
puts 'Published'
end
private
def search_params
params.permit(:message)
end
end
的JavaScript
<script src="https://js.pusher.com/4.2/pusher.min.js"></script>
<p>Have subscribed to a Slanger channel</p>
<p>Listening via WebSocket...</p>
<script type="text/javascript">
var pusher = new Pusher('application_key', {
wsHost: "localhost",
wsPort: "8080",
wssPort: "8080",
enabledTransports: ['ws', 'flash']
});
var channel = pusher.subscribe('MY_CHANNEL');
channel.bind('AN_EVENT', function(data) {
console.log('data: ', data);
});
Pusher.log = function(data) {
console.log('\t\t', data);
};
</script>
一切就绪后,您可以使用docker compose构建docker镜像并运行您的应用程序:
docker-compose build
docker-compose up
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e3996f55e7c helloslanger_web "bundle exec rails..." 13 seconds ago Up 10 seconds 0.0.0.0:3000->3000/tcp helloslanger_web_1
24c8b05f22a5 helloslanger_slanger "/bin/sh -c 'slang..." About an hour ago Up 13 seconds 0.0.0.0:8080->8080/tcp helloslanger_slanger_1
6baa9eec8c5a redis:4.0-alpine "docker-entrypoint..." 4 days ago Up 14 seconds 6379/tcp helloslanger_redis_1
46bc3498a5bf postgres "docker-entrypoint..." 4 days ago Up 14 seconds 5432/tcp helloslanger_db_1
现在打开订阅者URL:
http://localhost:3000/message/subscribe
并发布消息:
http://localhost:3000/message/publish/Slanger%20is%20cool
然后在订阅者的浏览器控制台中,您将看到:
Pusher : Event recd : {"event":"AN_EVENT","data":{"message":"Slanger is cool"},"channel":"MY_CHANNEL"}
data: {message: "Slanger is cool"}
希望它有所帮助。
顺便说一句,如果您想了解如何使用docker compose将Rails应用程序停靠,请参阅docker compose documentation。