mike chambers | about

Connecting Anonymously to an XMPP Group Chat using XIFF

I posted some code the other day that showed how to use the XIFF AS3 Library to connect to an XMPP server and join a group chat room.

Below is an slightly modified example that shows how to login anonymously, and connect to a room.

import org.jivesoftware.xiff.conference.Room;
import org.jivesoftware.xiff.core.JID;
import org.jivesoftware.xiff.core.XMPPSocketConnection;
import org.jivesoftware.xiff.events.LoginEvent;
import org.jivesoftware.xiff.events.RoomEvent;
import org.jivesoftware.xiff.events.XIFFErrorEvent;

private var connection:XMPPSocketConnection;

private function onCreationComplete():void
{
    connection = new XMPPSocketConnection();
    connection.useAnonymousLogin = true;
    connection.server = "mesh.local";
    connection.port = 5222;

    connection.addEventListener(LoginEvent.LOGIN, onLogin);
    connection.addEventListener(XIFFErrorEvent.XIFF_ERROR, onError);

    connection.connect("standard");
}

private function onLogin(e:LoginEvent):void
{
    var room:Room = new Room(connection);
    room.roomJID = new JID("test@conference.mesh.local");
    room.nickname = "frank";
    room.addEventListener(RoomEvent.ROOM_JOIN, onRoomJoin);
    room.join();
}

private function onRoomJoin(e:RoomEvent):void
{
    Room(e.target).sendMessage("im here");
}

private function onError(e:XIFFErrorEvent):void
{
    trace(e.errorCode);
}

The two differences are that you set:

connection.useAnonymousLogin = true;

and dont send a username and password. Once you join the room, you can then set your nick like so:

room.nickname = "frank";

I have set up a test chat room on my server if you want to play around with the code.

twitter github flickr behance