Permalink
Browse files

Added example of using sock.format('json') to the readme

  • Loading branch information...
1 parent fa3f1e1 commit 0d2114e68443df4f3c485e4d04743d6499f9016d @nisaacson nisaacson committed Mar 26, 2013
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 0d2114e

Please sign in to comment.