Alexandre Bourlier
Alexandre Bourlier

Reputation: 4118

Setting up ejabberd via websockets

I have an ejabberd server up and running. I can test it via web clients and it works fine using BOSH connections.

I would like to connect to it via web sockets now, and I am not sure what I am missing for it to work, I just know it doesn't.

Here is an extract from my ejabberd.yml

    hosts:
     - "localhost"
     - "somedomain.com"
     - "im.somedomain.com"

listen : 
    port: 5280
    ip: "::"
    module: ejabberd_http
    request_handlers:
      "/websocket": ejabberd_http_ws
      "/pub/archive": mod_http_fileserver
    web_admin: true
    http_bind: true
    ## register: true
    ## captcha: true
    tls: true
    certfile: "/etc/ejabberd/ejabberd.pem"

Now I tried to open a web socket via javascript as follows :

var ws = new WebSocket("ws://somedomain:5280/websocket/");

I get ERR_CONNECTION_TIMED_OUT in return. I have nothing within ejabberd's logs when I try to open a weksocket. I do have logs of the BOSH connections.

I am not sure if I am testing appropriately, nor if my server is setup correctly.

Any suggestion is most welcome.

Upvotes: 1

Views: 2218

Answers (1)

Sujan Kumar Sakthi
Sujan Kumar Sakthi

Reputation: 423

Connection timeout error will throw by the server when the client does not send pong response to the server make sure you are sending the pong response.If you are using Strophe.js kindly check Handlers http://strophe.im/strophejs/doc/1.2.14/files/strophe-js.html#Strophe.Connection.addHandler

    connection = new WebSocket("ws://somedomain:5280/websocket/");

//Adding ping handler using strophe connection
    connection.addHandler(pingHandler, "urn:xmpp:ping", "iq", "get");

//Ping Handler Call back function
    function pingHandler(ping) {
        var pingId = ping.getAttribute("id");
        var from = ping.getAttribute("from");
        var to = ping.getAttribute("to");
        var pong = strophe.$iq({
            type: "result",
            "to": from,
            id: pingId,
            "from": to
        });
        connection.send(pong);
        return true;
    }

Also, consider you are adding this configuration to your ejabberd.yml

websocket_ping_interval: 50
websocket_timeout: 60

Upvotes: 1

Related Questions