I want to send a notification to the client. For this, I use Redis pup/sub and ActionController::Live . This is what my StreamingController looks like:
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 is the JS part for listening to the stream:
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 click something on redis, the client receives a push notification. So it works great. But when I click on the link, nothing happens. After canceling the rails server (I use puma) with Ctrl + C , I got the following error:
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 after that I do not see the console exit after clicking on the client. config.cache_classes = true and config.eager_load = true are not parameters, because I do not want to restart my server every time during the development process.
Is there any other solution?
source share