Skip to content

Commit 58618c9

Browse files
committed
Add syntax highlighting to Readme
1 parent 4062670 commit 58618c9

1 file changed

Lines changed: 48 additions & 30 deletions

File tree

README.md

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ This library provides a simple, socket-oriented API* for messaging in
66
[node.js](http://nodejs.org/), using
77
[RabbitMQ](http://www.rabbitmq.com/) as a backend.
88

9-
var context = require('rabbit.js').createContext();
10-
context.on('ready', function() {
11-
var pub = context.socket('PUB'), sub = context.socket('SUB');
12-
sub.pipe(process.stdout);
13-
sub.connect('events', function() {
14-
pub.connect('events', function() {
15-
pub.write(JSON.stringify({welcome: 'rabbit.js'}), 'utf8');
16-
});
17-
});
9+
```js
10+
var context = require('rabbit.js').createContext();
11+
context.on('ready', function() {
12+
var pub = context.socket('PUB'), sub = context.socket('SUB');
13+
sub.pipe(process.stdout);
14+
sub.connect('events', function() {
15+
pub.connect('events', function() {
16+
pub.write(JSON.stringify({welcome: 'rabbit.js'}), 'utf8');
1817
});
18+
});
19+
});
20+
```
1921

2022
*Yes, rather like ZeroMQ. [See below](#zeromq).
2123

@@ -45,7 +47,9 @@ use RabbitMQ.
4547
The entry point is `createContext`, which gives you a factory for
4648
sockets. You supply it the URL to your RabbitMQ server:
4749

48-
var context = require('rabbit.js').createContext('amqp://localhost');
50+
```js
51+
var context = require('rabbit.js').createContext('amqp://localhost');
52+
```
4953

5054
A context emits `'error'` with an `Error` object if there's a problem
5155
with the underlying connection to the server. This invalidates the
@@ -56,13 +60,17 @@ emit `'close'` once the underlying connection has been terminated.
5660

5761
To start sending or receiving messages you need to acquire a socket:
5862

59-
var pub = context.socket('PUB');
60-
var sub = context.socket('SUB');
63+
```js
64+
var pub = context.socket('PUB');
65+
var sub = context.socket('SUB');
66+
```
6167

6268
and connect it to something:
6369

64-
pub.connect('alerts');
65-
sub.connect('alerts');
70+
```js
71+
pub.connect('alerts');
72+
sub.connect('alerts');
73+
```
6674

6775
Sockets act like ("old style")
6876
[Streams](http://nodejs.org/docs/v0.8.25/api/stream.html); in
@@ -71,24 +79,30 @@ and you can `write()` to those that are writable. If you're expecting
7179
data that is encoded strings, you can `setEncoding()` to get strings
7280
instead of buffers as data events.
7381

74-
sub.setEncoding('utf8');
75-
sub.on('data', function(note) { console.log("Alarum! " + note); });
76-
77-
pub.write("Emergency. There's an emergency going on", 'utf8');
82+
```js
83+
sub.setEncoding('utf8');
84+
sub.on('data', function(note) { console.log("Alarum! " + note); });
85+
86+
pub.write("Emergency. There's an emergency going on", 'utf8');
87+
```
7888

7989
You can also use pipe to forward messages to or from another stream,
8090
making relaying simple:
8191

82-
sub.pipe(process.stdout);
92+
```js
93+
sub.pipe(process.stdout);
94+
```
8395

8496
A socket may be connected more than once, by calling
8597
`socket.connect(x)` with different `x`s. What this entails depends on
8698
the socket type (see below), but messages to and from different
8799
`connect()`ions are not distinguished. For example
88100

89-
var sub2 = context.socket('SUB');
90-
sub2.connect('system');
91-
sub2.connect('notifications');
101+
```js
102+
var sub2 = context.socket('SUB');
103+
sub2.connect('system');
104+
sub2.connect('notifications');
105+
```
92106

93107
Here, the socket `sub2` will receive all messages published to
94108
`'system'` and all those published to `'notifications'` as well, but
@@ -99,7 +113,9 @@ Some socket types have options that may be set with
99113
sockets, which is message expiration, given as a stringified number of
100114
milliseconds:
101115

102-
pub.setsockopt('expiration', '60000')
116+
```js
117+
pub.setsockopt('expiration', '60000')
118+
```
103119

104120
In the example, messages written to `pub` will be discarded by the
105121
server if they've not been delivered after 60,000
@@ -143,13 +159,15 @@ A few modules have a socket-server-like abstraction; canonically, the
143159
`net` module, but also for example SockJS and Socket.IO. These can be
144160
adapted using something similar to the following.
145161

146-
var context = new require('rabbit.js').createContext('amqp://localhost');
147-
var inServer = net.createServer(function(connection) {
148-
var s = context.socket('PUB');
149-
s.connect('incoming');
150-
connection.pipe(s);
151-
});
152-
inServer.listen(5000);
162+
```js
163+
var context = new require('rabbit.js').createContext('amqp://localhost');
164+
var inServer = net.createServer(function(connection) {
165+
var s = context.socket('PUB');
166+
s.connect('incoming');
167+
connection.pipe(s);
168+
});
169+
inServer.listen(5000);
170+
```
153171

154172
This is a simplistic example; a bare TCP socket won't in general emit
155173
data in chunks that are meaningful to applications, even if they are

0 commit comments

Comments
 (0)