ZAKHOMUTHContact

← Writing

In Browser, Pure Javascript, XMPP Client

· From the 2010–2013 blog

So I was in research mode yesterday. I spent a bunch of time trying to figure out how to build an in browser chat engine that leveraged peoples already existing Facebook and GMail accounts. And I wanted to write this because it wasn't as intuitive as I thought it should be. So if you're in the market for setting up your own web or corporate chat heres what you need to know. Its all about the server When I approached it originally, I figured I could do everything I wanted in the client. Google and Facebook already have servers, why can't I just connect to those? Why can't I just have a bit of javascript that I open in a browser and it connects to google and facebooks servers - simple right? Well, it turns out there are 2 really decent reasons why you cant. First your javascript can only talk to stuff within your domain. This protection goes back a long way and was added, I guess, to reduce the likely-hood of cross-site scripting attacks. There a a couple work arounds like hacking it using dns and apache redirection, but its kinda painful. And second, because javascript cant open and hold real tcp connections you need to cheat when doing jabber/xmpp. The hack in this case is called bosh (which is basically just binding over http). this binding lets you fake these long held tcp connections, and do chat stuff without polling (which is the other solution). And because all we know if the google and facebook xmpp connection info, and not the bosh server info (if they even have bosh servers), we cant escape having our own server. OK, so what server do I use? Now this question was very debated and very heated 2 years ago when everyone and their brother was setting up chat on the coat-tails of google releasing googletalk. There are a couple picks, but as far as I can tell the world has united a bit on the choice.

  • jabberd2
    • a clone of the originally jabberd.
    • written in c
    • chose if you absolutely need to run a c-based bit of code
  • openfire
    • a semi-commercial jabber server
    • written in java
    • chose this is you absolutely need to run a java-based bit of code
  • tigase
    • also a jabber clone
    • written in java
    • chose this if you believe their claims about reliability
  • ejabberd
    • another jabber clone
    • written in erlang
    • its used by facebook, it can scale, its the most used, its the most complete, and its pretty easy to get going
    • chose this one

OK, Now I'm confused. I connect to a local jabber server with my google or facebook account? how does that even work? I can't really explain this one yet, I'm still working on figuring it out myself. But when you're setting up your local jabber server you need to tell it about all the other places you want to chat with, and you setup transports to allows that to happen. I still need to figure out how the remote login works, but I'm getting close. Basically I think your jabber server doubles as a proxy for these other services - allowing you to do the http binding and same-domain stuff. You setup accounts for everyone that onces to chat locally with an account you provide, and you proxy/transport all the other domains (ie. logging in with @google.com goes to the google servers, instead of yours). I'll let you know once I get it nailed down, but I'm close - and the important part is I have a good bubbly picture of the architecture. What about the client? So for the client there are a couple of great libraries and starting bits of code out there to get you going. There, as far as I can tell, isn't a great ready to use open-source client floating around, but check out these:

What else do I need to know? Here are some resources that might help - but you're basically up-to the point I'm at.