1
- # Node.js binding
1
+ # Zyre binding for Node.js
2
2
3
3
## Quick Start
4
4
5
+ This package wraps the Zyre library for Node.js. Here is an example of use:
6
+
5
7
```
6
- mkdir -p $HOME/temp
7
- cd $HOME/temp
8
- git clone https://github.com/zeromq/zyre
9
- cd zyre/bindings/nodejs
10
- ./build.sh
8
+ var ZyreBinding = require ('bindings')('zyre');
9
+ var zyre = new ZyreBinding.Zyre ();
10
+ console.log ("Create Zyre node, uuid=" + zyre.uuid () + " name=" + zyre.name ());
11
+
12
+ zyre.start ();
13
+ zyre.join ("GLOBAL");
14
+ zyre.print ();
15
+
16
+ while (true) {
17
+ var event = new ZyreBinding.ZyreEvent (zyre);
18
+ if (!event.defined ())
19
+ break; // Interrupted
20
+ event.print ();
21
+
22
+ if (event.type_name () == "ENTER") {
23
+ // If new peer, say hello to it and wait for it to answer us
24
+ console.log ("[" + event.peer_name () + "] peer entered");
25
+ zyre.whisper (event.peer_id (), "Hello");
26
+ }
27
+ else
28
+ if (event.type_name () == "EXIT") {
29
+ console.log ("[" + event.peer_name () + "] peer exited");
30
+ }
31
+ else
32
+ if (event.type_name () == "WHISPER") {
33
+ console.log ("[" + event.peer_name () + "] received ping (WHISPER)");
34
+ zyre.shout ("GLOBAL", "Hello");
35
+ }
36
+ else
37
+ if (event.type_name () == "SHOUT") {
38
+ console.log ("[" + event.peer_name () + "]("
39
+ + event.group () + ") received ping (SHOUT)");
40
+ }
41
+ event.destroy ();
42
+ }
43
+ zyre.stop ();
44
+ zyre.destroy ();
11
45
```
12
46
47
+ ## Implementation
13
48
49
+ This is a wrapping of the native C libzyre library. See binding.cc for the code.
14
50
15
- Problem: can't use Zyre from node.js
16
-
17
- Solution: start on binding for Zyre
51
+ We get two classes:
18
52
19
- Note: this an experiment. The real binding will be generated via
20
- zproject. However we will first make this binding work, and then
21
- use it as a template.
53
+ Zyre - a Zyre node instance
54
+ ZyreEvent - an event from the Zyre network
22
55
23
- Disclaimer: I am not an expert in Node, NAN, or C++, and this is
24
- probably horribly done. The work is not ready for use.
56
+ The Zyre class has these methods:
25
57
26
- ## How to build
27
-
28
- You need these repositories checked-out under a common root:
29
-
30
- * https://github.com/zeromq/zyre
31
- * https://github.com/zeromq/czmq
32
- * https://github.com/zeromq/libzmq
33
- * https://github.com/jedisct1/libsodium
34
-
35
- In zyre/bindings/nodejs,
36
-
37
- ```
38
- ./build.sh
39
- node test.js
40
- ```
58
+ destroy
59
+ defined
60
+ uuid
61
+ name
62
+ start
63
+ stop
64
+ setHeader (name, value)
65
+ setVerbose
66
+ join (group)
67
+ leave (group)
68
+ print
69
+ whisper (peer_id, message)
70
+ shout (group, message)
71
+ recv
41
72
42
- # Areas you can help in
73
+ The ZyreEvent class has these methods:
43
74
44
- * Making this work on Windows or OS/X
75
+ destroy
76
+ defined
77
+ type
78
+ type_name
79
+ peer_id
80
+ peer_name
81
+ peer_addr
82
+ header (name)
83
+ group
84
+ msg
85
+ print
45
86
46
- ## Hints for contributors
87
+ ## Building from Source
47
88
48
89
* You need Python (v2.7 recommended, v3.x not supported)
90
+ * You need (ideally) nvm and Node.js.
91
+ * If your Linux has an existing 'node' command, remove it. You need matching versions of node and node-gyp.
92
+ * In every terminal, or .bashrc: ` nvm use v5.5.0 `
49
93
50
- * You need (ideally) nvm and Node.js :
94
+ To install the necessary Node tools :
51
95
52
96
```
53
97
sudo apt-get update
@@ -60,7 +104,19 @@ npm install -g node-gyp
60
104
npm install -g node-pre-gyp
61
105
```
62
106
63
- * In every terminal, or .bashrc: ` nvm use v5.5.0 `
107
+ To build:
108
+
109
+ ```
110
+ mkdir -p $HOME/temp
111
+ cd $HOME/temp
112
+ git clone https://github.com/zeromq/zyre
113
+ cd zyre/bindings/nodejs
114
+ ./build.sh
115
+ ```
116
+
117
+ ## Making Changes
118
+
119
+ Note that this version of the binding is destined for destruction; we will replace it with one generated via zproject, with essentially the same API.
120
+
121
+ Having said that, fixes and improvements are welcome. Please send your pull requests to ` https://github.com/zeromq/zyre ` .
64
122
65
- * If your Linux has an existing 'node' command, remove it. You need
66
- matching versions of node and node-gyp.
0 commit comments