Message

Updated for v0.3.3

A message consists of zero or more properties and values to be sent to a client. Message is the lowest level of nengi's networking, and can be used to network just about any feature manually.

ExampleMessage

class ExampleMessage {
    constructor() {
        this.foo = 'hello'
        this.bar = 5
        this.baz = true
    }
}
ExampleMessage.protocol = {
    foo: nengi.String,
    bar: nengi.Number,
    baz: nengi.Boolean
}

After creating this definition, be sure to add it to the nengiConfig protocols.

nengiConfig.js

    messages: [
        ['ExampleMessage', require('./path/to/ExampleMessage')]
    ],

usage with an Instance

var msg = new ExampleMessage()
instance.message(msg, clientOrClients)

This message will then be sent to the specified client or clients (this can be a client object or it can be an array of client objects).

clientside

// reading a snapshot of data on the clientside
network.messages.forEach(message => {
    // { type: 0, foo: 'hello', bar: 5, baz: true, protocol: { name: 'ExampleMessage', ... }}
})

Note: the `type` added by nengi is used internally and won't make much sense to your game logic. Here's how to really identify the type of message:

// read
network.messages.forEach(message => {
    if (message.protocol.name === 'ExampleMessage') {
        // do stuff specific to ExampleMessage
    }
})

Message (advanced)

Instance-side message logic:

Each instance has a collection of clients. Each client has its own message queue. When instance.addMessage(msg, clientOrClients) or instance.messageAll(msg) is invoked, the message is added to these client-specific queues. When the instance builds its game snapshots for each, it empties these message queues and sends all messages to the respective clients.

Messages with zero properties:

Because messages have a 'type' added to them, even a message with zero properties still essentially has data. For example if we were to send a message called 'Disconnect' that denotes that a player was just disconnected, there would be no need to put any properties in that message. Just receiving that 'type' of message conveys our full meaning.