Persistent Network Connection from Rails Instance

Hi Everyone,

right now im building a rails app which needs to send xmpp (Jabber)
messages from time to time. Instead of connecting to the xmpp server
every time i want to send a message, i would like to establish a
persistent connection when the rails instance is started and use this
connection every time a message needs to be sent. What would be the
recommended way to do this in Rails. My Ideas so far are to implement
the xmpp code either as a plugin or using some kind of BackgrounDRb
task. What would you think is the best way to do it?

Thanks!

Florian

Hello Florian,

I look at this the same way as handling outgoing e-mail. Queue your
emails and have an outside process consume the queue. In the case of
emails, I use ar_mailer, which could be used as a template for sending
xmpp instead of email. It’s much easier than setting up
BackgroundDRb.

One question to answer is about timeliness. With email, it may be
fine to have your background process consume the queue every few
minutes or so. It’s nice when your email for a “forgot password”
shows up in your inbox seconds after the request, but a minute or two
may be alright. With instant messaging, the word “instant” kind of
stands out. So waiting a minute or two for the message may not be
acceptable…especially for a chat application. If you are just
sending server status messages and don’t care about a slight delay,
I’d go the ar_mailer way (message queue, cron job to process queue).

Good Luck,
Jim

I just realized you wanted the persistant connection. The ar_mailer
has the daemonize feature I believe, so this where I’d focus my
thoughts on making the server connection and maintaining it with a
daemonized process that checks the message queue. It might even be
interesting to use drb from rails to ask the daemonized process to
check the queue for a new message so that you don’t have to query for
new messages every minute.

Sorry about that.

-Jim

Florian wrote:

right now im building a rails app which needs to send xmpp (Jabber)
messages from time to time. Instead of connecting to the xmpp server
every time i want to send a message, i would like to establish a
persistent connection when the rails instance is started and use this
connection every time a message needs to be sent. What would be the
recommended way to do this in Rails. My Ideas so far are to implement
the xmpp code either as a plugin or using some kind of BackgrounDRb
task. What would you think is the best way to do it?

One way is to use a class variable in the controller which needs it.

Either create the connection in your config/environment.rb file, or add
a custom init method to the controller and set it there, like:

class MyController < ApplicationController
def initialize *args
# make sure we do the normal startup stuff
super
# now stuff for our specific class
@@xmpp_connection = …
end
:

You don’t need to worry about the actual arguments expected. They may
change in future versions anyway. Using *args allows for whatever they
may be and giving no arguments to #super will use any args that are
passed.

@@xmpp_connection = ...

end

Thank you Mark, that`s what i was looking for!

Florian