-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremote-actor.js
56 lines (50 loc) · 1.56 KB
/
remote-actor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
/**
* To successfully run this example, you need to first launch a Comedy listener
* node on this host. To do that, run:
*
* $ node_modules/.bin/comedy-node
*
* You can also launch a listener node on the host of your choice and change the
* remote IP address in this example (remoteIpAddress variable) to check real
* network communication.
*
* To start listener node on remote machine, do:
*
* $ npm install comedy
* $ node_modules/.bin/comedy-node
*
* in any folder on remote machine.
*/
var actors = require('comedy');
// Remote IP address. You can change it to a different IP address.
// If Comedy node is started and accessible on that address, the example will work.
var remoteIpAddress = '127.0.0.1';
/**
* Actor definition class.
*/
class MyActor {
sayHello(to) {
// Reply with a message, containing self PID.
return `Hello to ${to} from ${process.pid}!`;
}
}
// Create an actor system.
var actorSystem = actors();
actorSystem
// Get a root actor reference.
.rootActor()
// Create a class-defined child actor, that is run on a remote machine (remote mode).
.then(rootActor => rootActor.createChild(MyActor, { mode: 'remote', host: remoteIpAddress }))
// Send a message to our remote actor with a self process PID.
.then(myActor => myActor.sendAndReceive('sayHello', process.pid))
.then(reply => {
// Output result.
console.log(`Actor replied: ${reply}`);
})
// Log errors.
.catch(err => {
console.error(err);
})
// Destroy the system, killing all actor processes.
.finally(() => actorSystem.destroy());