I have this code:
def create
message = Message.new(text: params[:message][:text], author: params[:message][:author])
if message.save
render json: {result: 'success'}
else
render json: {result: 'failure'}
end
end
I have client subscribed to Faye Server:
var subscription = client.subscribe('/foo', function (message) {
getMessages();
});
I want to publish some message to Faye when a message is created. As listed in Faye Ruby Sever documentation, I have to do like this:
require 'eventmachine'
EM.run {
client = Faye::Client.new('http://localhost:9292/faye')
client.subscribe('/foo') do |message|
puts message.inspect
end
client.publish('/foo', 'text' => 'Hello world')
}
But when I paste this code into my create
method, it blocks the rails thread with EventMachine and server doesn't work any more.
When I use client.publish
without EventMachine
, I get an error.
How can I publish to Faye from server? I know that there are gems like faye-rails
or private_pub
, but I want to figure out how to do it myself. Is there any way to integrate EventMachine and Rails? Maybe I should run EventMachine on a different thread?
I did't use Event Machine but I use Fay-web Socket in rails , I am using thin web-server for my application to show notification.
First you add this line into you Gemfile
In my case I am using this inside the **Notification.rb to sending notification.**
Example
For subscriber
Now ! Run your faye.ru file using terminal
For details Faye websocket