Permalink
Browse files

Merge pull request #92 from nisaacson/master

Add sock.format('json') to the readme
  • Loading branch information...
tj committed Mar 26, 2013
2 parents fa3f1e1 + 0d2114e commit 0d9d289822a8e6bc4d3e9a6fa7f95474af3ce9e0
Showing with 36 additions and 2 deletions.
  1. +36 −2 Readme.md
View
@@ -283,12 +283,47 @@ axon.codec.define('json', {
encode: JSON.stringify,
decode: JSON.parse
});
-```
__Note:__ codecs must be defined on both the sending and receiving ends, otherwise
axon cannot properly decode the messages. You may of course ignore this
feature all together and simply pass encoded data to `.send()`.
+To use a codec in a socket pair, use the `format(<codec name>)` command. For example, to send json over a req/rep socket pair
+
+```
+var axon = require('axon'),
+ requestSocket = axon.socket('req'),
+ replySocket = axon.socket('rep');
+
+requestSocket.format('json')
+requestSocket.bind(3000);
+
+replySocket.format('json')
+replySocket.connect(3000)
+
+replySocket.on('message', function (data, cb) {
+ // data is a json object
+ console.log('reply socket received json data')
+ console.dir(data)
+ var output = {
+ fizz: 'buzz'
+ }
+ cb(null, output)
+})
+var data = {
+ foo: 'bar'
+}
+
+requestSocket.send(data, function(err, res){
+ if (err) {
+ console.dir(err)
+ return
+ }
+ console.log('request socket callback received data')
+ console.dir(res)
+})
+```
+
## Performance
Preliminary benchmarks on my Macbook Pro based on 10 messages
@@ -372,4 +407,3 @@ $ make test
## License
MIT
-

0 comments on commit 0d9d289

Please sign in to comment.