Skip to content

Commit 53158b1

Browse files
committed
Merge pull request #426 from hintjens/master
Publishing zyre.node on npm
2 parents 8586e27 + 20391ac commit 53158b1

File tree

2 files changed

+94
-40
lines changed

2 files changed

+94
-40
lines changed

bindings/nodejs/README.md

+92-36
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,97 @@
1-
# Node.js binding
1+
# Zyre binding for Node.js
22

33
## Quick Start
44

5+
This package wraps the Zyre library for Node.js. Here is an example of use:
6+
57
```
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 ();
1145
```
1246

47+
## Implementation
1348

49+
This is a wrapping of the native C libzyre library. See binding.cc for the code.
1450

15-
Problem: can't use Zyre from node.js
16-
17-
Solution: start on binding for Zyre
51+
We get two classes:
1852

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
2255

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:
2557

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
4172

42-
# Areas you can help in
73+
The ZyreEvent class has these methods:
4374

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
4586

46-
## Hints for contributors
87+
## Building from Source
4788

4889
* 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`
4993

50-
* You need (ideally) nvm and Node.js:
94+
To install the necessary Node tools:
5195

5296
```
5397
sudo apt-get update
@@ -60,7 +104,19 @@ npm install -g node-gyp
60104
npm install -g node-pre-gyp
61105
```
62106

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`.
64122

65-
* If your Linux has an existing 'node' command, remove it. You need
66-
matching versions of node and node-gyp.

bindings/nodejs/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"name": "zyre",
3-
"version": "0.0.1",
4-
"private": true,
3+
"version": "0.0.3",
54
"description": "Zyre",
6-
"main": "index.js",
75
"scripts": {
86
"test": "echo \"Error: no test specified\" && exit 1"
97
},
@@ -15,6 +13,6 @@
1513
"url": "git://github.com/zeromq/zyre.git"
1614
},
1715
"dependencies": {
18-
"nan": "^2.2.0",
16+
"nan": "^2.2.0"
1917
}
2018
}

0 commit comments

Comments
 (0)