Client manager

A client is anything that is connected to the PushJS platform. This could be a browser, native app or even a backend script.
The client object holds all information about the attributes the client has, the channels the client resides, etc. Clients can be found throught the client manager. This manager enables you to send private messages, listen to attribute changes or simply, just fetch a client.

Methods


Name
Parameters
getClientById
(int) clientId
setAttribute
(string) name, (string) value
addEventListener
(string) event, (function) callback
sendPrivateMessage
(string) clientId, (string) event, (string) message

Client ID

Problably one of the most important things to know, in order to build great applications, is to know that every
connected client has it's own client ID. Nobody is stopping you from adding your own custom id of course in the attributes.
Knowing this makes it very easy to get and use the connected client.

Take a look at the following example, it shows a system event listener that listens for new clients that entered the channel.
Let's say it is a small multiplayer game and every new player must be added to the playfield.

                
<script>

    function ready() {

        var channelId = "GREAT_SCOTT";

        PushJS.channelManager.createChannel(channelId);
        PushJS.channelManager.joinChannel(channelId);

        PushJS.channelManager.addEventListener(PushJS.events.CLIENT_ENTER, onClientEnter);
    }

    function onClientEnter(client) {

        // every new client that just entered the channel
        // is now inserted to the playfield because of the
        // event listener calling this function
        var player = document.createElement('div');

        player.id = client.getClientID();
        player.className = 'player';

        document.getElementById('playfield').appendChild(player);
    }

</script>
                
            

Client attributes

You can set or get the attributes of each client very easily. Take a look at the following code example

                
<script>

    function setPlayerHealth(health) {
        PushJS.me.setAttribute('health', health);
    }

    setPlayerHealth(75);

</script>
                
            

This code example show how to set an attribute called `health`. When updating or setting an attribute, the PushJS server
will dispatch an event called CLIENT_ATTRIBUTE_CHANGED. All clients connected to this channel will receive this event.
When receiving this event, your application can act upon it and change the player health bar for example.

To receive client attribute updates, simply add the correct system event listener. When the event is dispatched and received in the callback function
only the changed attribute is being sent as parameter.

                
<script>

    function ready() {

        var channelId = "USE_THE_FORCE_PICARD";

        PushJS.channelManager.createChannel(channelId);
        PushJS.channelManager.joinChannel(channelId);

        // add a client attribute changed listener
        PushJS.clientManager.addEventListener(PushJS.events.CLIENT_ATTRIBUTE_CHANGED, onAttributeChanged);
    }

    function onAttributeChanged(fromClient, name, value) {

        var clientId = fromClient.getClientID();

        if (name === 'color') {
            console.log('OMG did you see his new color, it is ' + color);
        }
    }

</script>
                
            

Putting everything together

See this complete example that ties everything together.
View the source here or go the live examples and see the mini game.