Skip to content

Client API Reference

Using a CDN

MassiveRealm provides three versions of the client SDK, which can be included in your project as follows:

Browser Version

html
<script src="https://cdn.massiverealm.com/js/v1/client-browser.js" type="text/javascript"></script>

Usage:

javascript
window.MR = new window.MassiveRealmClient.Client({
...
});

Standard Version

html
<script src="https://cdn.massiverealm.com/js/v1/client.js" type="text/javascript"></script>

Usage:

javascript
let MRClient = new MassiveRealmClient.Client({
...
});

npm Package

If you are using Node.js, you can install the client SDK via npm.

More information: https://www.npmjs.com/package/massiverealm-client

Run the following command to install the package:

bash
npm install massiverealm-client

Usage:

javascript
import { Client as MassiveRealmClient } from 'massiverealm-client';

let MRClient = new MassiveRealmClient({
    ...
});

Basic Usage

Initialization

To initialize the client, create a new instance of MassiveRealmClient.Client:

html
<script src="https://cdn.massiverealm.com/js/v1/client-browser.js" type="text/javascript"></script>

<script type="text/javascript">
    window.MR = new window.MassiveRealmClient.Client({
        debug: true,
        url: 'https://your-project-url/',
        publicKey: 'your-public-key',

        location: null, // null for automatic location detection, or "EU" for Europe, "US" for United States, "AP" for Asia-Pacific.

        autoReconnect: true, // Enable auto-reconnect.

        autoReconnectMaxAttempts: 3, // Maximum number of auto-reconnect attempts.

        autoReconnectTimeout: 3000, // Auto-reconnect timeout in milliseconds.

        onConnect: function() {
            console.log('Connected to server');
        },

        onDisconnect: function() {
            console.log('Disconnected from server');
        },

        onCommand: function(command, data) {
            console.log('Received command:', command, 'with data:', data);

            if (command == 'Pong') {
                console.log(data);
            }
        },

        onError: function(error) {
            console.error('Error:', error);
        },

        onReconnectAttempt: function(attempt) {
            console.log('Reconnect attempt:', attempt);
        },

        onMessage: function(buffer) {
            console.log('Received message:', buffer);
        },

        onJoinRoom: function(roomAlias, roomId, isForwarded) {
            console.log('Joined room:', roomAlias, 'with ID:', roomId, 'forwarded:', isForwarded);
        },

        onRoomFull: function(roomAlias, roomId) {
            console.log('Room is full:', roomAlias, 'with ID:', roomId);
        },

        onLeaveRoom: function() {
            console.log('Left room');
        }
    });
</script>

Connecting to the Server

Call the connect method to establish a connection to the server:

javascript
window.MR.connect();

Joining a Room

Use the joinRoom method to join a specific room:

javascript
window.MR.joinRoom({
    alias: 'room-alias', // Define this alias in your project settings
    id: 'optional-room-id' // You can provide a specific room ID or let the server generate it
});

Sending Commands

To send commands to the server, use the emit method:

javascript
window.MR.emit('commandName', {
    param1: 'value1',
    param2: 'value2'
});

Sending Multiple Commands

Batching operations is a good way to reduce the number of messages sent and avoid network overhead.

To send multiple commands at once, use the emit method with an array of commands:

javascript
window.MR.emit([
    {
        command: 'commandName',
        params: {
            param1: 'value1',
            param2: 'value2'
        }
    },
    {
        command: 'commandName2',
        params: {
            keyA: 'valueA',
            keyB: 'valueB'
        }
    }
]);

Example

Please check our Quick Start Guide for a complete example of using the client API.

Performance Tips

Check out our Performance Tips for optimizing your project. Pay attention to the Export Room Settings.