Get Started / Web

Build a messaging app

You can access Buddycloud in many ways. The following steps quickly introduce you on how to exchange posts in a channel node, which is the most basic thing you can do with Buddycloud.

This example uses Javascript (better suited for web apps) but you can use our REST API in order to build other apps such as mobile ones.

Sign up for a test account / Install Buddycloud

To start building Buddycloud you will need a test account on the Dev-Console. We recommend using the developer console initially and later, you can install your own Buddycloud stack.

Include primus.js

You will need to include this script otherwise none of the steps below will work.

<script src="https://demo.buddycloud.com/scripts/primus/primus.js" type="text/javascript"></script>

Create a user

The following Javascript code snippet creates an account: .

var socket = new Primus("https://demo.buddycloud.com");
socket.on('open', function() {
    socket.send('xmpp.login',
    {
        jid: '',
        password: '',
        register: true
    });
});

socket.on('xmpp.connection', function() {
    window.alert("User account created successfully!");
});

Connect, authenticate, discover

The following Javascript code snippet is pretty important and required so that you can perform the further steps described in the following sections in this document. This is how you connect to your Buddycloud domain, authenticate your user and then discover the Buddycloud server.

var socket = new Primus("https://demo.buddycloud.com");
socket.on('open', function() {
    socket.send('xmpp.login',
    {
        jid: '',
        password: ''
    });
});

socket.on('xmpp.connection', function() {
    socket.send('xmpp.buddycloud.discover', {}, function(error, data) {
        window.alert("Authenticated as your user successfully!");
    });
});

Fetch posts

The following Javascript code snippet assumes you are already authenticated and also that you have already discovered the Buddycloud server. It retrieves posts from the [email protected]/posts node.

socket.send('xmpp.buddycloud.retrieve',
{
    node: '/user/[email protected]/posts',
    rsm: { max: 10 }
},
function(error, posts) {
    if (!error) {
        /* handle display of existing posts */
        output = "";
        posts.reverse().forEach(function(post) {
            var author = post.entry.atom.author.name;
            var content = post.entry.atom.content.content;
            output += "Author: " + author + "; Msg: " + content + "\n";
        });
        window.alert("Fetched posts successfully!\n"+output);
    }
});

Post something yourself

The following Javascript code snippet assumes you are already authenticated and that you have already discovered the Buddycloud server. It subscribes your user to the [email protected]/posts node and then creates a post on this node. Finally, it starts listening for incoming messages (and thus will receive the one you've just posted).

socket.send('xmpp.buddycloud.subscribe',
{
    node: '/user/[email protected]/posts'
},
function(error, data) {
    socket.send('xmpp.buddycloud.publish',
    {
        node: '/user/[email protected]/posts',
        content: {
            atom: {
                content: "Hello world!"
            }
        }
    },
    function(error, posts) {
        if (!error) {
            window.alert("Post created successfully!");
        }
    });
});

socket.on('xmpp.buddycloud.push.item', function(post) {
    if ( post.node === '/user/[email protected]/posts' ) {
        var author = post.entry.atom.author.name;
        var content = post.entry.atom.content.content;
        window.alert("You were notified of new post!\nAuthor: "+author+"; Msg: "+content);
    }
});
Back to Top