Channel manager

What is the channel manager

There is already a lot of text passed by talking about channels. But what are they exactly? And how do they fit in the PushJS idea?
A channel is a public or private space where clients can connect to and exchange information in the form of events/messages and event/message listeners. This is achieved by using the channelManager object.

Methods


Name
Parameters
createChannel
(string) channelId, (object) settings
joinChannel
(string) channelId, (string) password
addEventListener
(string) channelId, (string) event, (function) callback
getChannel
(string) channelId

Create a channel

Before joining a channel, it is a good idea to create a channel. Don't worry, if you join a channel it will be automatically be created if it does not exist.

                
<script>

    function ready() {

        // create a new channel with a maximum of 2 clients
        // remove the channel when it is empty
        var settings = {
            removeOnEmpty: true,
            maxClients: 2
        };

        PushJS.channelManager.createChannel(channelId, settings);
    }

</script>
                
            

Channel attributes

Just like a client, a channel also has it's own attributes. Some of them can be set and some of them are already present.
Use the createChannel method only if you want to create some specific channel for example with maximum of 2 clients allowed.
Or to create a room that is only accessible by a password.
Normally if you send a message through a channel that does not exist, the channel will be created automatically for you

                
<script>

    function ready() {

        // create a new channel with a maximum of 2 clients
        // remove the channel when it is empty
        var attributes = {
            removeOnEmpty: true,
            maxClients: 2
        };

        PushJS.channelManager.createChannel('my_private_channel', attributes);
    }

</script>
                
            

Password protected channel

If your channel has to be even more private, a password can be set. Only clients with the
correct password can then enter the channel and fully participate.

                
<script>

    function ready() {

        // create a new channel with a maximum of 2 clients
        // remove the channel when it is empty
        var attributes = {
            password: 'helloworld'
        };

        PushJS.channelManager.createChannel('my_private_channel', attributes);
    }

</script>
                
            

Joining a channel

Joining a channel is even more easier than creating a channel. After a channel is created, a client can join the channel.

                
<script>

    function ready() {
        var channelId = "AWESOME_CHANNEL";

        PushJS.channelManager.createChannel(channelId);

        PushJS.channelManager.joinChannel(channelId);
    }

</script>