xmpp4js - Code Samples

Information

In addition to these snippets, check out the SimpleClient demo in the xmpp4js-launcher project.

Core

Create a connection

  1. var con = new JabberConnection( "/http-bind/""soashable.com" );  
  2. con.beginSession();  

Close a connection

Login

  1. con.authenticateMd5( "username""password""resource" );  

Register

  1. var reg = new Packet.Registration();  
  2. reg.setNode( "myjid" );  
  3. reg.setPassword( "mypass" );  
  4. reg.setEmail( "somebody@somewhere.com" );  
  5. reg.setName( "My Name" );  
  6.   
  7. con.send( reg );  

Send an IM

  1. var msg = new Packet.Message("somebody@somewhere.com""hello");  
  2. con.send( msg );  

Use Chat Manager

  1. var cm = Xmpp4Js.Chat.ChatManager.getInstanceFor( con );  
  2.   
  3. cm.setOptions({  
  4.     ignoreThread: true// useful for legacy networks (AIM)  
  5.     ignoreResource: true   
  6. });  
  7.   
  8. cm.on({  
  9.     scope : this,  
  10.     chatStarted : onChatStarted,  
  11.     messageReceived : onChatMessageReceived  
  12. });  
  13.   
  14. function onChatStarted(chat) {  
  15.     alert( "Chat with "+chat.getParticipant()+" started." );  
  16. }  
  17.   
  18. function onChatMessageReceived(chat, messagePacket) {  
  19.     alert( "New message from "+messagePacket.getFrom()+": "+messagePacket.getBody() );  
  20. }  
  21.   
  22. function sendMessage(to, message) {  
  23.     var chat = null;  
  24.     try {  
  25.         chat = chatManager.findBestChat( to );  
  26.     } catch(e) {  
  27.         chat = chatManager.createChat( to );  
  28.     }  
  29.       
  30.     var messagePacket = new Xmpp4Js.Packet.Message( to, "normal", message );  
  31.     chat.sendMessage( messagePacket );  
  32. }  

Pause / Resume a connection

  1. // this assumes ExtJs is present for the state manager. what   
  2. // actually needs to happen is an x-domain state manager needs  
  3. // to be implemented so that state can carry across domains.  
  4.   
  5. // setup  
  6. LOGIN = "test";  
  7. PASSWORD = "test";  
  8. SEND_MSG_TO = "harlan@soashable.com";  
  9. BOSH_TRANSPORT = Xmpp4Js.Transport.Script;  
  10. BOSH_ENDPOINT = "http://bosh*.soashable.com:7070/http-bind/";  
  11.   
  12. Ext.state.Manager.setProvider( new Ext.state.CookieProvider() );  
  13.   
  14. // 1, 4  
  15. sp = new Xmpp4Js.Packet.StanzaProvider();  
  16. sp.registerDefaultProviders();  
  17. con = new Xmpp4Js.Connection({transport: {clazz: BOSH_TRANSPORT,endpoint:BOSH_ENDPOINT,useKeys:true},stanzaProvider:sp});  
  18. con.on( "pause"function(pauseStruct) {   
  19.   console.dir( pauseStruct );   
  20.   Ext.state.Manager.set( "pauseStruct", pauseStruct );  
  21. });  
  22. con.on( "resume"function(pauseStruct) {  
  23.   con.send( new Xmpp4Js.Packet.Message( SEND_MSG_TO, "normal""yoooo I'm alive" ) );  
  24. });  
  25. con.on( "connect"function() {   
  26.   loginFlow = new Xmpp4Js.Workflow.Login({con:con});  
  27.   loginFlow.on("success"function() {  
  28.     con.send( new Xmpp4Js.Packet.Presence() );  
  29.   } );  
  30.   loginFlow.start( "plaintext", LOGIN, PASSWORD );  
  31. });  
  32.   
  33. // 2  
  34. con.connect("soashable.com");  
  35.   
  36. // 3 - pause for up to 120 seconds. at this point you can reload the page.  
  37. con.pause( 120 );  
  38.   
  39. // 5 - you should receive an IM  
  40. ps = Ext.state.Manager.get( "pauseStruct" );  
  41. con.resume( ps );  
  42.       

Privacy, Presence

Change my presence

  1. var pres = new Packet.Presence( "available""dnd" );  

Change my presence for only a specific buddy

  1. var pres = new Packet.Presence( "available""dnd" );  
  2. pres.setTo( "buddy@soashable.com" );  

Capture others' presence

  1. con.addPacketListener( function(presence) {  
  2.     var from = presence.getFrom();  
  3.     var type = presence.getType();  
  4.     var status = presence.getStatus();  
  5.     var awayMsg = presence.getShow();  
  6.     if( type == "available" ) {  
  7.       alert( from + " is " + status + "; show=" + awayMsg );  
  8.     } else {  
  9.       alert( from + " is offline." );  
  10.     }  
  11. }, new PacketClassFilter( Packet.Presence ) );  

Add a buddy

  1. var roster = con.getRoster();  
  2. roster.createEntry( "mybuddy@soashable.com""My Buddy", ["group a""group b"] );  

Remove a buddy

... there is no way right now. ouch...

Confirm/Deny a request to be added as a buddy

  1. con.addPacketListener( function(presence) {  
  2.     if( presence.getType() == "subscribe" ) {  
  3.       var allowed = confirm( "Do you want to let " + presence.getTo() + " subscribe to your presence?");  
  4.       var outPres = new Packet.Presence( "subscribed" );  
  5.       outPres.setTo( presence.getFrom() );  
  6.       con.send( outPres );  
  7.     }  
  8. }, new PacketClassFilter( Packet.Presence ) );  

Events, Listeners

Be notified of message events (composing)

  1. con.addPacketListener(  
  2.     function(msg) {  
  3.       // load extensions that are present in the packet using our ExtensionProvider  
  4.       msg.loadExtensions(extensionProvider);  
  5.   
  6.       var msgEvent = msg.getExtension( MessageEventExtension.XMLNS );  
  7.       alert( "Message Event: " + msg.getTo() + " - " + msgEvent .getEvent() );  
  8.   
  9.     },   
  10.     // capture only Message packets with MessageEventExtension  
  11.     new AndFilter(  
  12.       new PacketClassFilter( Packet.Message ),   
  13.       new ExtensionFilter( MessageEventExtension.XMLNS )  
  14.     )   
  15. );  

Respond to version requests

  1. con.addPacketListener( function(iq) {  
  2.     var versionIq = new Packet.IQ( iq.getFrom(), "set""jabber:iq:version" );  
  3.     versionIq.getQuery().textContent = "Rockin' Jabber Client";  
  4.     con.send( versionIq );  
  5. }, new IQQueryNSFilter ( "jabber:iq:version" ) );  

Extensions

Sign in to AIM

  1. TransportHelper.registerForAim( "aim.im.soashable.com""screen name""password" );  

-or-

  1. var reg = new Packet.Registration();  
  2. reg.setTo( "aim.soashable.com" );  
  3. reg.setNode( "aimscreenname" );  
  4. reg.setPassword( "mypass" );  
  5.   
  6. con.send( reg );  

Store private data

  1. var ds = con.getDataStorage();  
  2. ds.set( "mycompany:greeting""Hello""greeting" );  

Retrieve private data

  1. var ds = con.getDataStorage();  
  2. ds.get( "mycompany:greeting"function(responseNodes ) {  
  3.     var greeting = responseNodes[0].textContent;  
  4.     alert( greeting );  
  5. }, "greeting" );  

Conference

Join a Multi-User Chat (MUC)

  1. var extProvider = ...;  
  2. var mucMan = MucManager.getInstanceFor( con, "conference.soashable.com", extProvider );  
  3.   
  4. // get info about a room and create a state to act with  
  5. var room = mucMan.getRoom( "soashable" ).createState();  
  6.   
  7. // join with the nick "harlan2"  
  8. room.join( "harlan2" );  

Get the status of a Join

  1. // use extjs's Ext.util.Observable for registering events  
  2. room.on({  
  3.   join: function(room, participant, packet) { },  
  4.   error: function(room, participant, packet) {  
  5.     console.dirxml( packet.getExtension( ErrorExtension.XMLNS ).getNode() );  
  6.   },  
  7. });  
  8.   
  9. room.join( "harlan2" );  

Send Message to a MUC

  1. room.sendText( "Hello, MUC!" );  

List Occupants

  1. // getting participants happens asynchronously, so it  
  2. // does NOT simply return a list.  
  3. room.getParticipants(function(room, participants) {  
  4.     console.dir( participants );  
  5. });  

Leave a MUC

Getting a list of MUCs

  1. mucMan.getRoomList(function(mucMan, rooms) {  
  2.     console.dir( rooms );  
  3. });  

About xmpp4js

xmpp4js is an xmpp connection library for Javascript. It is written with a heavy emphasis on resuability and extensibility, and is naturally heavilly tested.

About Xmpp4Js

Developer Docs

Project Documentation

Powered By