Easy to use xml-free javascript XMPP library based on Strophe.js.
POWERCHAT_! is a simple to use XMPP library for webapps and messengers directly in the browser. With POWERCHAT_! you are able to use the whole world of XMPP extensions without dealing with xml. POWERCHAT_! uses two functions, to convert xml to json and back. You only have to send and receive simple javascript objects.
POWERCHAT_! can use bosh and websocket connections.
You first need a Jabber-ID from a XMPP server. This is mostly like get a email address. Your Jid looks like this:
With your beloved web search engine you will find a lot of free XMPP server to use. When you have a account you can sign in with your Jid and your password on every XMPP messenger out there. You could try https://conversejs.org/ for example.
Include POWERCHAT_! and the latest strophe.min.js: (Get strophe.min.js from here)
<script src='strophe.min.js'></script>
<script src='POWERCHAT_!'></script>
XMPP.OnMessage = function ( message ) {
alert ( "New Message from " + message.from + ": " + message.body_text );
}
XMPP.OnConnectionStatus = console.info;
XMPP.OnPresence = console.log;
You need to know the bosh or websocket address from your server.
XMPP.connect ( {
connection: "wss://example.com/websocket", // Your bosh / websocket connection
jid: "[email protected]",
pass: "aliceLovesBob"
} );
////// Console output will be:
// 1 "CONNECTING"
// 5 "CONNECTED"
XMPP.OnConnectionStatus will be triggered multiple times. When you are connected, you will receive messages and presences from your buddys.
XMPP.send ( {
to: "[email protected]",
body_text: "Hello, whats up?"
} );
XMPP.sendPresence ( {
show_text: "xa"
} );
With XMPP you can also manage a contact list called "roster". For this you should first add the namespace, described in the official XMPP documentation and define two callback-functions.
Strophe.addNamespace('ROSTER', 'jabber:iq:roster');
success_callback = function ( roster ) {
alert ( "Roster received!" );
console.log ( roster );
};
error_callback = console.error;
XMPP.sendIQ ( { query_xmlns: Strophe.NS.ROSTER }, success_callback, error_callback );
The roster will also be simple json. The xml result would be:
<iq id='bv1bs71f'
to='[email protected]'
type='result'>
<query xmlns='jabber:iq:roster' ver='ver7'>
<item jid='[email protected]'/>
<item jid='[email protected]'/>
</query>
</iq>
But the result you will get in the success_callback function is simple this:
{
id: 'bv1bs71f',
to: '[email protected]',
type: 'result',
query_xmlns: 'jabber:iq:roster',
query_ver: 'ver7',
query_items: [ { jid: '[email protected]' }, { jid: '[email protected]' } ],
}
You see that you have all informations from the basic XMPP stanza but it is allready converted into an javascript object.
It is now easy to use an XMPP extension, called XEP, without writing a plugin for the library. Just visit the Extension documentation from XMPP.org: https://xmpp.org/extensions/ and pick the XEP you want to use.
For example XEP-0085: Chat State Notifications:
Strophe.addNamespace('CHATSTATE', 'http://jabber.org/protocol/chatstates');
XMPP.send ( {
to: "[email protected],
composing_xmlns: Strophe.NS.CHATSTATE
} );
Will connect you to a server. The transport-protocol for this connection will be chosen automatically based on the given service parameter. URLs starting with “ws://” or “wss://” will use WebSockets, URLs starting with “http://”, “https://” or without a protocol will use BOSH. To make Strophe connect to the current host you can leave out the protocol and host part and just pass the path.
The cookies option allows you to pass in cookies to be added to the document. These cookies will then be included in the BOSH XMLHttpRequest or in the websocket connection.
The passed in value must be a map of cookie names and string values.
As the connection process proceeds, the user supplied callback will be triggered multiple times with status updates. The callback should take two arguments - the status code and the error condition.
The status code will be one of the values in the Strophe.Status constants. The error condition will be one of the conditions defined in RFC 3920 or the condition ‘strophe-parsererror’.
The Parameters wait, hold and route are optional and only relevant for BOSH connections. Please see XEP 124 for a mor detailed explanation of the optional parameters.
- parametersObject with following keys:
- (String) service:
- (String) jid: The user’s JID. This may be a bare JID, or a full JID. If a node is not supplied, SASL OAUTHBEARER or SASL ANONYMOUS authentication will be attempted (OAUTHBEARER will process the provided password value as an access token).
- (String) pass: The user’s password.
- (Function) callback: The connect callback function.
- (Integer) wait: The optional HTTPBIND wait value. This is the time the server will wait befor returning an empty result for a request. The default setting of 60 seconds is recommended.
- (Integer) hold: The optional HTTPBIND hold value. This is the number of connections the server will hold at one time. This should almost always be set to 1 (the default).
- (String) route: The optional route value.
- (String) authcid: The optional alternative authentication identity (username) if intending to impersonate another user. When using the SASL-EXTERNAL authentication mechanism, for example with client certificates, then the authcid value is used to determine whether an authorization JID (authzid) should be sent to the server. The authzid should not be sent to the server if the authzid and authcid are the same. So to prevent it from being sent (for example when the JID is already contained in the client certificate), set authcid to that same JID. See XEP-178 for mor details.
Will disconnect you from the server.
Will send a message. You need at least two ###### Parameters:
- messageObject.to: Who receives the message
- messageObject.body_text: The message text
Will send a presence. All parameters are optional. To send an certain presence, set show_text.
Will send a query to a server.
- (object) iq: A javascript object as a query.
- (function) success_callback: Will be triggered, when the server answers to the query. It returns a javascript object with the answer.
- (function) error_callback: Will be triggered, when the server answers with an error. It returns a javascript object with the error.
- (int) timeout: The time for timeout. By default this is the value in XMPP.timeout, initialized with 5000.
Sets the debug messages in the console on or off.
- (boolean) debugOn
A useful function to get a subnode. For example to transform:
XMPP.getSubNode( {
carbon_forwarded_message_body_text: "...",
carbon_forwarded_message_from: "..."
}, "message");
// output:
// {
// body_text: "...",
// from: "..."
// }
Is the default timeout for IQ's.
If you are familiar with Strophe.js you could find this useful. It is the link to a full Strophe connection. You are free to use certain Strophe.js plugins from here.
Is the currenct status of your connection to the server. Each status has a number:
- "ERROR"
- "CONNECTING"
- "CONNFAIL"
- "AUTHENTICATING"
- "AUTHFAIL"
- "CONNECTED"
- "DISCONNECTED"
- "DISCONNECTING"
- "ATTACHED"
- "REDIRECT"
- "CONNTIMEOUT"
- "REGIFAIL"
- "REGISTER"
- "REGISTERED"
- "CONFLICT"
- "NOTACCEPTABLE"
Will be triggered when the connection status is changing.
- StatusID: Is a number from 0 to 15 and related to the status
- Status: A readable string
Will be triggered when you are connected.
Will be triggered when you are disconnected.
Will be triggered when you receive a message.
- object.from: Who sent the message
- object.type: Mostly "chat" or "groupchat"
- object.to: Who receives the message
- object.body_text: The message text
- object...
Will be triggered when you receive a presence.
- object.from: Who sent the message
- object.show: Will be null or object.show_text will have the presence
- object...
Will be triggered when you receive something.
- inputObject: The XML input converted into a javascript object.
Will be triggered when you send something.
- outputObject: The XML input converted into a javascript object.