sono.land

on

on

Create a custom event listener

The 'on' method allows developers to create custom event listeners. When a user calls a function with a unique event name, the recipient can listen for that event using the 'on' method. The recipient will then be able to invoke a callback associated with the custom event name.

Client Side

1/* In this example, a custom direct message method is created to send a
2private message. */
3
4// Client #1 - sends a direct message along with the 'DM" event
5document.getElementById('sendDM').addEventListener('click', () => {
6 const message = document.getElementById('input').value;
7 const client = document.getElementById('client').value;
8 sono.directMessage(message, client, 'DM');
9});
10
11/* Client #2 - listens for the 'DM' event using the 'on' method and creates
12a custom handler for this event */
13const messageBoard = document.getElementById('privatemessageBoard');
14
15sono.on('DM', (payload) => {
16 const message = document.createElement('li');
17 message.innerText = `FROM: ${payload.from} Message: ${payload.message}`;
18 messageBoard.appendChild(message);
19});
Edit this page on GitHub