# Persistent Network Connection from Rails Instance

**URL:** <https://www.ruby-forum.com/t/persistent-network-connection-from-rails-instance/131025>\
**Category:** Rails\
**Created:** [March 5, 2008, 10:34am UTC](https://www.ruby-forum.com/t/persistent-network-connection-from-rails-instance/131025 "2008-03-05T10:34:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![florian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://www.ruby-forum.com/u/florian)\
**Post date:** [March 5, 2008, 10:34am UTC](https://www.ruby-forum.com/t/persistent-network-connection-from-rails-instance/131025/1 "2008-03-05T10:34:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![florian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://www.ruby-forum.com/u/florian)\
**Post date:** [March 5, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/persistent-network-connection-from-rails-instance/131025/2 "2008-03-05T15:13:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![florian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://www.ruby-forum.com/u/florian)\
**Post date:** [March 5, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/persistent-network-connection-from-rails-instance/131025/3 "2008-03-05T15:27:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![florian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://www.ruby-forum.com/u/florian)\
**Post date:** [March 5, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/persistent-network-connection-from-rails-instance/131025/4 "2008-03-05T16:52:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![florian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/898d66/32.png) [@florian](https://www.ruby-forum.com/u/florian)\
**Post date:** [March 6, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/persistent-network-connection-from-rails-instance/131025/5 "2008-03-06T20:31:42Z")

</div>

> ```
> @@xmpp_connection = ...
> 
> ```
> 
> end

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

Florian
