# Establish Connection to a xmpp server and keep it!

**URL:** <https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814>\
**Category:** Rails\
**Created:** [December 12, 2007, 1:35pm UTC](https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814 "2007-12-12T13:35:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 12, 2007, 1:35pm UTC](https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814/1 "2007-12-12T13:35:54Z")

</div>

Hello,

(shamelessly adapted to my needs from a old message from this list)

I want to integrate status messages over xmpp in my Rails application.  
My problem is that I do not want to establish a connection to the  
jabber server each time a message is sent and disconnect the  
connection afterwards. So I am looking for a solution to establish the  
connection and use this connection in my controllers when I need it. I  
am going to use xmpp4r to communicate with the jabber network.

How can I use the connection im my controllers?

Will there be some issues about multiple application instances  
(because of FastCGI, Mongrel, etc.) ?

With best regards,  
Mickael

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 12, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814/2 "2007-12-12T15:25:08Z")

</div>

On 12 Dec 2007, at 09:43, MickTaiwan wrote:

> am going to use xmpp4r to communicate with the jabber network.
> 
> How can I use the connection im my controllers?

You’re going to some sort of persistent process to maintain this. Call  
this an xmpp daemon, it will be the process (not a mongrel or fastcgi  
instance) in charge of talking to the jabber server(s).  
You then need to decide how you’re going to talk to this process. You  
could use drb, you could have a table in the database that this  
process looks at (something like a ‘pending\_status\_updates’ table),  
etc…  
You might also want to look at the latest version of backgroundrb, the  
advantage there is that they’ve figured out communication etc… for  
you.

Fred

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 12, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814/3 "2007-12-12T17:32:34Z")

</div>

I don’t know if I am the lucky post you adapted but I posted something  
similar a while back. I am still working on that project now. I am using  
XMPP4R to communicate with a Jabber server we are running. It is a rails  
application and I am using Drb to communicate with the Jabber server  
persistently. It works pretty well. Feel free to email me - xgamerx10  
(at) gmail (dot) com if you have any specific questions or want to talk  
about it.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 13, 2007, 8:12am UTC](https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814/4 "2007-12-13T08:12:04Z")

</div>

I’ve taken this approach (creating an XMPP worker in BackgroundRB). In  
my case, I need a proxy for many users on our sites, so this middle-  
ware needs to pool many connections to jabber servers and let the  
Rails app poll for data once in a while (for things like presence  
updates).

BackgroundRB is going through major changes right now, but making fast  
progress, so it is worth checking out and getting on the mailing list.

There have been a number of threads on this topic [http://railsforum.com/](http://railsforum.com/)  
too.

If others are working on Jabber/XMPP middle layers, it would be great  
to compare notes.

-chris

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 12, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814/5 "2007-12-12T22:27:33Z")

</div>

Thanks Frederick for your reply, it is exactly what I needed to know  
(do I have to keep the connection outside my controller, the answer is  
yes).

Nathan, the post I adapted is from alexander at [kueken.net](http://kueken.net)  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/6e054869c5e09eb1/](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/6e054869c5e09eb1/)

Thanks for your help.

I will play a bit with backgroundrb, and implement a worker to keep  
xmpp connections.

Mickael.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 15, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814/6 "2007-12-15T20:36:42Z")

</div>

Hi,

Dec 13, 2007 2:57 AM, MickTaiwan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I will play a bit with backgroundrb, and implement a worker to keep  
> xmpp connections.

Hey there,

With latest version of BackgrounDRb you can have tcp connections  
integrated within event loop of your worker. Your worker would hold  
the connection and you can pass messages to worker from rails easily.

The capability is already there, not just documented on how to have  
tcp client/server connections from a bdrb worker. But this I have  
rectified. Ping me on bdrb mailing list, I would be glad to help you  
out on this.

–  
Let them talk of their oriental summer climes of everlasting  
conservatories; give me the privilege of making my own summer with my  
own coals.

[http://gnufied.org](http://gnufied.org)

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [December 13, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/establish-connection-to-a-xmpp-server-and-keep-it/122814/7 "2007-12-13T20:15:54Z")

</div>

On Dec 13, 8:11 am, chaupt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If others are working on Jabber/XMPP middle layers, it would be great  
> to compare notes.

or source code ? 🙂
