[英]Rails, ActionController::Live, Puma: ThreadError
I want to stream notification to the client. 我想将通知流式传输给客户端。 For this, I use Redis pup/sub
and the ActionController::Live
. 为此,我使用Redis pup/sub
和ActionController::Live
。 Here is what my StreamingController
looks like: 这是我的StreamingController
样子:
class StreamingController < ActionController::Base
include ActionController::Live
def stream
response.headers['Content-Type'] = 'text/event-stream'
$redis.psubscribe("user-#{params[:user_id]}:*") do |on|
on.pmessage do |subscription, event, data|
response.stream.write "data: #{data}\n\n"
end
end
rescue IOError
logger.info "Stream closed"
ensure
response.stream.close
end
end
Here the JS part to listen to the stream: 这里是JS部分来监听流:
var source = new EventSource("/stream?user_id=" + user_id);
source.addEventListener("message", function(e) {
data = jQuery.parseJSON(e.data);
switch(data.type) {
case "unread_receipts":
updateUnreadReceipts(data);
break;
}
}, false);
Now if I push something to redis, the client gets the push-notification. 现在,如果我将某些内容推送到redis,客户端将获得推送通知。 So this works fine. 所以这很好。 But when I click on a link nothing is happening. 但是,当我点击链接时,没有任何事情发生。 After canceling the rails server (I use puma) with Ctrl + C I got the following error: 用Ctrl + C取消rails服务器(我使用puma)后出现以下错误:
ThreadError: Attempt to unlock a mutex which is locked by another thread
The problem can be solved after adding config.middleware.delete Rack::Lock
to development.rb, but then I don't see any console output after pushing to the client. 将config.middleware.delete Rack::Lock
添加到development.rb后可以解决问题,但是在推送到客户端后我看不到任何控制台输出。 config.cache_classes = true
and config.eager_load = true
are no options because I don't want to restart my server every time in development. config.cache_classes = true
和config.eager_load = true
是没有选项的,因为我不希望每次开发时都重启我的服务器。
Is there any other solution? 还有其他解决方案吗?
如果您想避免重新启动服务器以获取更改,那么我认为您需要运行多个进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.