
Messages
========

The `Messages` module allows CEF applications to communicate with each other by using the `_spaces.setNotifier` API.

Below is an example on how to register/send messages in two CEF applications:

### Example

#### Application A (Message Receiver)

```JS
import Promise from "bluebird";
import { registerMessage } from "spaces-utils/messages";

// Register a message `currentTime` as client `App_A`. You should make sure the client name is unique among
// all other CEF applications, and keep using the same client name throughout.
registerMessage("App_A", "currentTime", (_, done) => {
    // Invoke the `done` callback to notify the sender with the passed in result.
    done(null, Date.now());

    // Or pass in a string in the first param to signal an error.
    // done("Unable to get current time");
});

// Now register a second message "echo" that reply back the params it receives after 1s delay.
// This demonstrates how to process a message asynchronously.
registerMessage("App_A", "echo", (descriptor, done) => {
    Promise.resolve()
        .delay(1000)
        .then(() => done(null, descriptor));
});
```

#### Application B (Message Sender)

```JS
import { sendMessage } from "spaces-utils/messages";

// Send message "currentTime" to Application A via its client name "App_A". Once the message is processed,
// the promise should be resolved with the current time string.
sendMessage("App_A", "currentTime")
    .then(time => console.info("The time now is", time))
    .catch(err => console.info("Client failed to process the message:", err));

// Send message "echo" to Application A
sendMessage("App_A", "echo", { "hello": "world" })
    .then(results => console.info(results));
```
