4

I am trying to use a pushing mechanism using websocket-rails gem in ROR application.

I have basically done the folowing.

my application.js

//= require jquery
//= require jquery_ujs
//= require websocket_rails/main

$(function(){

  // connect to server like normal
  var dispatcher = new WebSocketRails('localhost:3000/websocket');

  // subscribe to the channel
  var channel = dispatcher.subscribe('products');

  // bind to a channel event
  channel.bind('new', function(data) {
    console.log('channel event received: ' + data);
  });
});

then I started thin server at port 3000

Then I from rails console, I entered the following command.

WebsocketRails[:products].trigger(:new, Product.last)

But nothing was printed in browser console.

Have I missed some setup configuration?

Thanks

2 Answers 2

2

I came across this too. The documentation says:

Broadcast to the channel from anywhere inside your Rails application. An existing controller, a model, a background job, or a new WebsocketRails controller.

i.e. no mention of the console.

In my experience, it works exactly as described fine from the controller though.

Sign up to request clarification or add additional context in comments.

2 Comments

It is true, I have the same problem and it works fine when I put the code in a controller.
cant I use it on a model?
0

I know this question is quite old, but there is some update on the project, so for everyone coming here in the future, now using Synchronization between instances events can be triggered from everywhere, even from rails console.

Add this to your config/initializers/websocket_rails.rb

WebsocketRails.setup do |config|
  config.synchronize = true
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.