Event manager

What is the event manager

Methods


Name
Parameters
dispatchEvent
(string) channelId, (string) event, (string) message
dispatchFilteredEvent
(array) filters, (string) channelId, (string) event, (string) message
dispatchPrivateEvent
(int) clientId, (string) event, (string) message

Dispatching events

The event manager let's you dispatch events to one or more clients. Depending on the parameters of the call, it can send it to one client, all clients in a channel or a set of specific clients. The basic event dispatch will send a message to all the other clients in the channel that the dispatching client currently has joined.

In the below example, the HELLO_WORLD_EVENT is dispatched in the MY_CHANNEL channel. All clients in this channel will receive the event. The "hello world" message is a simple message as example. JSON is also an option to send as a message.

                
<script>

    let myChannel = 'MY_CHANNEL';
    let myEvent = 'HELLO_WORLD_EVENT';

    PushJS.eventManager.dispatchEvent(
        myChannel,
        myEvent,
        'hello world!'
    );

</script>
                
            

Using filters

With filters it is possible to target a specific group of clients. The below example demonstrates how to target all the clients that have
the attribute color with a value of 'red'.

                
<script>

    let event = 'RED_EVENT';

    let filters = [
        {
            name: 'color',
            value: 'red'
        }
    ];

    PushJS.eventManager.dispatchFilteredEvent(filters, event, JSON.stringify({
            x: 100,
            y: 100
        })
    );

</script>
                
            

Targeting only one client

This strategy can be used for private messaging for example. Another usage could be in a multiplayer game to reach only one player that should undertake an action. Using this method will only reach the client from the given id.

                
<script>

    let clientId = 1;
    let event = 'EPIC_BONUS_EVENT';

    PushJS.eventManager.dispatchFilteredEvent(clientId, event, JSON.stringify({
            weapon: 'BFG'
        })
    );
</script>