Configuring BOSH support on a Prosody XMPP server

Prosody XMPP/Jabber logo In my last article on this topic I brought a brief introduction into installing XMPP server Prosody on you Debian/Ubuntu based server. In this follow-up article I'll show you how to configure Bidirectional-streams Over Synchronous HTTP (BOSH) XMPP extension on Prosody XMPP server with the intention of creating XMPP powered web applications.

Introduction

Native transport protocol for XMPP is TCP. What this means is that XMPP requires two way communication channel for exchanging stanzas with our XMPP server. If we want to emulate this type of channel over HTTP protocol for building web applications, our XMPP server must support Bidirectional-streams Over Synchronous HTTP (BOSH) extension described inside XEP-0124. Prosody XMPP server supports this functionality out the box but it is disabled by default because it requires additional configuration to HTTP server. In this article I'll be using Apache server but same approach can be adopted to other HTTP server platforms.

Apache configuration

General idea is to create HTTP endpoint to be used by our web application from client side to exchange stanzas with our XMPP server using Ajax. By default Prosody will expose BOSH endpoint on port 5280, but due to same-origin policy implemented by most browser we can't use this endpoint as is. Instead we need to create port 80 endpoint and configure HTTP server to proxy all requests to this endpoint to port 5280. When using Apache our first step is to enable mod_proxy and mod_proxy_http Apache modules like this:

sudo a2enmod proxy proxy_http

Next step is to open our Apache virtual host file and define our proxy. We will use /http-bind as our endpoint and proxy all requests from http://localhost/http-bind/ to http://localhost:5280/http-bind/ and vice versa. As an example I'll open default virtual host file bundled with most Debian based distributions:

sudo nano /etc/apache2/sites-available/default

Here's contents of this file after our modifications (delimited by # BOSH endpoint proxy START and # BOSH endpoint proxy END tags):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
 
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
 
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>
 
        ErrorLog ${APACHE_LOG_DIR}/error.log
 
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
 
        CustomLog ${APACHE_LOG_DIR}/access.log combined
 
        # BOSH endpoint proxy START
        <IfModule mod_proxy.c>
                <IfModule mod_proxy_http.c>
                        ProxyRequests Off
                        ProxyPass /http-bind http://localhost:5280/http-bind/
                        ProxyPassReverse /http-bind http://localhost:5280/http-bind/
                </IfModule>
        </IfModule>
        # BOSH endpoint proxy END
 
</VirtualHost>

Now we restart Apache service and proceed to Prosody configuration.

sudo service apache2 restart

Prosody configuration

Next task is to open Prosody configuration file using our favourite text editor, in this case I'll use nano:

sudo nano /etc/prosody/prosody.cfg.lua

First thing is to uncomment following line inside modules_enabled to enable BOSH support:

"bosh"; -- Enable BOSH clients, aka "Jabber over HTTP"

Next is to define BOSH port (5280 in our case) and BOSH maximum inactivity timeout:

-- BOSH port
bosh_ports = { 5280 }
 
-- BOSH maximum inactivity
bosh_max_inactivity = 60

Now we restart prosody service:

sudo service prosody restart

To test is everything in order we just examine response from our endpoint using web browser or CURL:

curl -i http://localhost/http-bind/

Expected response is:

HTTP/1.1 200 OK
Date: Fri, 09 Aug 2013 22:07:04 GMT
Content-Type: text/html
Content-Length: 93
Vary: Accept-Encoding
 
<html><body>You really don't look like a BOSH client to me... what do you want?</body></html>

Now you can proceed with creating your XMPP BOSH powered web application using libraries like my XMPP BOSH - PHP - jQuery.

Enjoy!

DevGenii

A quality focused Magento specialized web development agency. Get in touch!

5 thoughts on “Configuring BOSH support on a Prosody XMPP server

  1. Kevin

    Hi!, After following your tutorial and succesfully setting up my bosh server, I’m trying Conversejs to make a simple web chat application, but when trying to connect from my computer’s localhost to my server, I get a message saying:

    XMLHttpRequest cannot load https://MYSITE.com/http-bind. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8080’ is therefore not allowed access.

    (I removed my own server for security purposes) Could you lend me a hand here?
    Thanks!

    Reply
    1. Marko Author

      Hello,
      what you describe should be taken care of through configuring reverse proxy as described. If this isn’t working, it’s 99% due to reverse proxy not working, so give it another look. Especially pay attention to having “proxy proxy_http” modules, without them, reverse proxy won’t work.

      Regards

      Reply
  2. Rahul

    Hello Marko,

    I am trying one webrtc application video chat,text chat, voice chat using prosody xmpp server library. But Actually I dont know how prosody server is working with webrtc and how to use as a signalling part in webrtc. Please help me in this part.

    Reply

Leave a Reply to Rouis Cancel reply

Your email address will not be published. Required fields are marked *