Blather with Rails

267 views
Skip to first unread message

Vignesh

unread,
Feb 8, 2014, 3:59:52 AM2/8/14
to xmpp-b...@googlegroups.com
Hi,
I'm new to blather. How do I combine Blather with my Rails app?

I found this example app using Blather with Sinatra. https://github.com/vanstee/linkbot It looks like blather is separated from the Sinatra app.

Should Blather always be separated from the webapp or can it be integrated with the webapp(like adding a script to application.rb)? I'm looking at using Blather with a Rails app. How do I integrate Blather with rails so I can send messages just like sending mail in Action Mailer.

Thanks,
Vignesh

Ben Langfeld

unread,
Feb 8, 2014, 8:57:12 AM2/8/14
to xmpp-b...@googlegroups.com
You can start Blather in a thread in a Rails initializer, store a reference to the client in a global of some sort, and send messages using the normal API.


--
You received this message because you are subscribed to the Google Groups "Blather" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xmpp-blather...@googlegroups.com.
To post to this group, send email to xmpp-b...@googlegroups.com.
Visit this group at http://groups.google.com/group/xmpp-blather.
For more options, visit https://groups.google.com/groups/opt_out.

Vignesh

unread,
Feb 8, 2014, 9:09:40 AM2/8/14
to xmpp-b...@googlegroups.com
Thanks for replying, Can you give me an example?
Has any project on github used Blather with Rails? I tried googling but I couldn't find any.

-Vignesh

Ben Langfeld

unread,
Feb 8, 2014, 9:16:53 AM2/8/14
to xmpp-b...@googlegroups.com
I don't personally know of any examples, no.

Me Vignesh

unread,
Feb 8, 2014, 12:09:38 PM2/8/14
to xmpp-b...@googlegroups.com
Thanks Ben. I seem to have found a good example, a rails app using xmpp4r. I don't think its not much different from a rails app using blather. Now I can get the pieces together.

https://github.com/ging/social_stream/wiki/Getting-Started-With-Social-Stream-Presence


--
You received this message because you are subscribed to a topic in the Google Groups "Blather" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/xmpp-blather/e4sGLXQBS8U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to xmpp-blather...@googlegroups.com.

To post to this group, send email to xmpp-b...@googlegroups.com.
Visit this group at http://groups.google.com/group/xmpp-blather.
For more options, visit https://groups.google.com/groups/opt_out.



--
Regards,
Vignesh

Vignesh

unread,
Feb 9, 2014, 12:47:30 PM2/9/14
to xmpp-b...@googlegroups.com
Hi Ben,
I'm stuck with a problem.
I wrote a initializer file in rails with the following contents.

client = {}
Thread.abort_on_exception = true
Thread.new do
  require 'blather/client'
 
  client = setup "client@localhost", "password"

  msg = Blather::Stanza::Message.new 'client-1@localhost', 'Testing msg... '

  when_ready {
    p 'Writing to stream'
    write_to_stream msg
  }
end

It produces the following error message.

rails s
=> Booting WEBrick
=> Rails 4.0.2 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
Run with script/rails [options] user@server/resource password [host] [port]
    -D, --debug                      Run in debug mode (you will see all XMPP communication)
    -d, --daemonize                  Daemonize the process
        --pid=[PID]                  Write the PID to this file
        --log=[LOG]                  Write to the [LOG] file instead of stdout/stderr
        --certs=[CERTS DIRECTORY]    The directory path where the trusted certificates are stored
    -h, --help                       Show this message
    -v, --version                    Show version
/home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:226:in `const_get': uninitialized constant [[[], nil]]::User (NameError)
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:226:in `block in constantize'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `each'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `inject'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `constantize'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise.rb:297:in `get'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise/mapping.rb:77:in `to'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise/mapping.rb:72:in `modules'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise/mapping.rb:89:in `routes'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise/mapping.rb:156:in `default_used_route'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise/mapping.rb:66:in `initialize'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise.rb:331:in `new'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise.rb:331:in `add_mapping'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise/rails/routes.rb:218:in `block in devise_for'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise/rails/routes.rb:217:in `each'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/lib/devise/rails/routes.rb:217:in `devise_for'
    from /srv/ruby/rails/courses/config/routes.rb:3:in `block in <top (required)>'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:341:in `instance_exec'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:341:in `eval_block'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.2/lib/action_dispatch/routing/route_set.rb:319:in `draw'
    from /srv/ruby/rails/courses/config/routes.rb:1:in `<top (required)>'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:40:in `each'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/file_update_checker.rb:75:in `call'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/file_update_checker.rb:75:in `execute'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:6:in `execute_if_updated'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/vignesh/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from /home/vignesh/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from /home/vignesh/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from /home/vignesh/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from /home/vignesh/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from /home/vignesh/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from /home/vignesh/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /srv/ruby/rails/courses/config/environment.rb:5:in `<top (required)>'
    from /srv/ruby/rails/courses/config.ru:3:in `block in <main>'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
    from /srv/ruby/rails/courses/config.ru:in `new'
    from /srv/ruby/rails/courses/config.ru:in `<main>'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands/server.rb:48:in `app'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands/server.rb:75:in `start'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands.rb:76:in `block in <top (required)>'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands.rb:71:in `tap'
    from /home/vignesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands.rb:71:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

I found out that this problem occours as soon as I require 'blather/client' . I even tried doing the require on the main thread, even then I face the same problem.

I tried to run just the contents of the Thread as a separate file using ruby(not in rails) and it ran successfully, it sent the message.

Please help,

-Vignesh

Ben Langfeld

unread,
Feb 9, 2014, 1:20:31 PM2/9/14
to xmpp-b...@googlegroups.com
Take a look at the following example for how to use Blather without being a standalone script: https://github.com/adhearsion/blather/blob/develop/examples/ping_pong.rb

I'll get some better documentation into the README on this soon, it's a very common question.


--

Me Vignesh

unread,
Feb 10, 2014, 5:18:06 AM2/10/14
to xmpp-b...@googlegroups.com
Thanks Ben,
Every thing is working now.

If some one is interested in using Blather with Rails take a look at the following code segment

class XmppClient
  include Blather::DSL

  def initialize(username, domain, resource, password, host)
    self.setup username+"@"+domain+"/"+resource, password, host
  end
end
 
client = XmppClient.new(username, domain, resource, password, host)
client.when_ready {
  puts "Xmpp connection established"
}

client.subscription :request? do |s|
  write_to_stream s.approve!
end
 
client.message :chat?, :body do |m|
  say m.from, "You sent: #{m.body}"
end

Myapp::Application.config.client = client

 
Thread.abort_on_exception = true
Thread.new  do
  EM.run { client.run }
end


Add in add this to the controller/model were u want to send a message

    msg = Blather::Stanza::Message.new
    msg.to = user.email+'@'+domain
    msg.body = "Some text"
    Myapp::Application.config.client.write_to_stream msg

-Vignesh


--
You received this message because you are subscribed to a topic in the Google Groups "Blather" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/xmpp-blather/e4sGLXQBS8U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to xmpp-blather...@googlegroups.com.

To post to this group, send email to xmpp-b...@googlegroups.com.
Visit this group at http://groups.google.com/group/xmpp-blather.
For more options, visit https://groups.google.com/groups/opt_out.

Chad Lancour

unread,
Mar 5, 2014, 3:57:32 PM3/5/14
to xmpp-b...@googlegroups.com
Thanks Vingesh, that's exactly the context I was looking for.

And thanks BEN! for a nice stable, jruby compatible 1.0 release! 
Reply all
Reply to author
Forward
0 new messages