Message

Updated for v1.0.0

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', ExampleMessage]
    ],

usage with an Instance

const 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 => {
    // { ntype: 0, foo: 'hello', bar: 5, baz: true, protocol: { name: 'ExampleMessage', ... }}
})

Note: the `ntype` 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 'ntype' added to them, even a message with zero properties is still meangful. 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 the message conveys our full meaning.

You can simulate channel-like behavior by creating an array of clients and sending messages to that specific group (e.g. Red team or Blue Team). A full channel api is being added to nengi in a future version.

Messages are not synced to interpolation frames, and thus *appear* to arrive at the client faster than entity changes.