In addition to these snippets, check out the SimpleClient demo in the xmpp4js-launcher project.
var con = new JabberConnection( "/http-bind/", "soashable.com" ); con.beginSession();
var reg = new Packet.Registration(); reg.setNode( "myjid" ); reg.setPassword( "mypass" ); reg.setEmail( "somebody@somewhere.com" ); reg.setName( "My Name" ); con.send( reg );
var cm = Xmpp4Js.Chat.ChatManager.getInstanceFor( con ); cm.setOptions({ ignoreThread: true, // useful for legacy networks (AIM) ignoreResource: true }); cm.on({ scope : this, chatStarted : onChatStarted, messageReceived : onChatMessageReceived }); function onChatStarted(chat) { alert( "Chat with "+chat.getParticipant()+" started." ); } function onChatMessageReceived(chat, messagePacket) { alert( "New message from "+messagePacket.getFrom()+": "+messagePacket.getBody() ); } function sendMessage(to, message) { var chat = null; try { chat = chatManager.findBestChat( to ); } catch(e) { chat = chatManager.createChat( to ); } var messagePacket = new Xmpp4Js.Packet.Message( to, "normal", message ); chat.sendMessage( messagePacket ); }
// this assumes ExtJs is present for the state manager. what // actually needs to happen is an x-domain state manager needs // to be implemented so that state can carry across domains. // setup LOGIN = "test"; PASSWORD = "test"; SEND_MSG_TO = "harlan@soashable.com"; BOSH_TRANSPORT = Xmpp4Js.Transport.Script; BOSH_ENDPOINT = "http://bosh*.soashable.com:7070/http-bind/"; Ext.state.Manager.setProvider( new Ext.state.CookieProvider() ); // 1, 4 sp = new Xmpp4Js.Packet.StanzaProvider(); sp.registerDefaultProviders(); con = new Xmpp4Js.Connection({transport: {clazz: BOSH_TRANSPORT,endpoint:BOSH_ENDPOINT,useKeys:true},stanzaProvider:sp}); con.on( "pause", function(pauseStruct) { console.dir( pauseStruct ); Ext.state.Manager.set( "pauseStruct", pauseStruct ); }); con.on( "resume", function(pauseStruct) { con.send( new Xmpp4Js.Packet.Message( SEND_MSG_TO, "normal", "yoooo I'm alive" ) ); }); con.on( "connect", function() { loginFlow = new Xmpp4Js.Workflow.Login({con:con}); loginFlow.on("success", function() { con.send( new Xmpp4Js.Packet.Presence() ); } ); loginFlow.start( "plaintext", LOGIN, PASSWORD ); }); // 2 con.connect("soashable.com"); // 3 - pause for up to 120 seconds. at this point you can reload the page. con.pause( 120 ); // 5 - you should receive an IM ps = Ext.state.Manager.get( "pauseStruct" ); con.resume( ps );
var pres = new Packet.Presence( "available", "dnd" ); pres.setTo( "buddy@soashable.com" );
con.addPacketListener( function(presence) { var from = presence.getFrom(); var type = presence.getType(); var status = presence.getStatus(); var awayMsg = presence.getShow(); if( type == "available" ) { alert( from + " is " + status + "; show=" + awayMsg ); } else { alert( from + " is offline." ); } }, new PacketClassFilter( Packet.Presence ) );
var roster = con.getRoster(); roster.createEntry( "mybuddy@soashable.com", "My Buddy", ["group a", "group b"] );
con.addPacketListener( function(presence) { if( presence.getType() == "subscribe" ) { var allowed = confirm( "Do you want to let " + presence.getTo() + " subscribe to your presence?"); var outPres = new Packet.Presence( "subscribed" ); outPres.setTo( presence.getFrom() ); con.send( outPres ); } }, new PacketClassFilter( Packet.Presence ) );
con.addPacketListener( function(msg) { // load extensions that are present in the packet using our ExtensionProvider msg.loadExtensions(extensionProvider); var msgEvent = msg.getExtension( MessageEventExtension.XMLNS ); alert( "Message Event: " + msg.getTo() + " - " + msgEvent .getEvent() ); }, // capture only Message packets with MessageEventExtension new AndFilter( new PacketClassFilter( Packet.Message ), new ExtensionFilter( MessageEventExtension.XMLNS ) ) );
TransportHelper.registerForAim( "aim.im.soashable.com", "screen name", "password" );
-or-
var reg = new Packet.Registration(); reg.setTo( "aim.soashable.com" ); reg.setNode( "aimscreenname" ); reg.setPassword( "mypass" ); con.send( reg );
var extProvider = ...; var mucMan = MucManager.getInstanceFor( con, "conference.soashable.com", extProvider ); // get info about a room and create a state to act with var room = mucMan.getRoom( "soashable" ).createState(); // join with the nick "harlan2" room.join( "harlan2" );
// use extjs's Ext.util.Observable for registering events room.on({ join: function(room, participant, packet) { }, error: function(room, participant, packet) { console.dirxml( packet.getExtension( ErrorExtension.XMLNS ).getNode() ); }, }); room.join( "harlan2" );
xmpp4js is an xmpp connection library for Javascript. It is written with a heavy emphasis on resuability and extensibility, and is naturally heavilly tested.