4 [![Build Status](https://secure.travis-ci.org/socketio/socket.io-parser.svg?branch=master)](http://travis-ci.org/socketio/socket.io-parser)
5 [![NPM version](https://badge.fury.io/js/socket.io-parser.svg)](http://badge.fury.io/js/socket.io-parser)
7 A socket.io encoder and decoder written in JavaScript complying with version `4`
8 of [socket.io-protocol](https://github.com/socketio/socket.io-protocol).
9 Used by [socket.io](https://github.com/automattic/socket.io) and
10 [socket.io-client](https://github.com/automattic/socket.io-client).
14 socket.io-parser is the reference implementation of socket.io-protocol. Read
16 [socket.io-protocol](https://github.com/learnboost/socket.io-protocol).
20 ### Encoding and decoding a packet
23 var parser = require('socket.io-parser');
24 var encoder = new parser.Encoder();
30 encoder.encode(packet, function(encodedPackets) {
31 var decoder = new parser.Decoder();
32 decoder.on('decoded', function(decodedPacket) {
33 // decodedPacket.type == parser.EVENT
34 // decodedPacket.data == 'test-packet'
35 // decodedPacket.id == 13
38 for (var i = 0; i < encodedPackets.length; i++) {
39 decoder.add(encodedPackets[i]);
44 ### Encoding and decoding a packet with binary data
47 var parser = require('socket.io-parser');
48 var encoder = new parser.Encoder();
50 type: parser.BINARY_EVENT,
51 data: {i: new Buffer(1234), j: new Blob([new ArrayBuffer(2)])}
54 encoder.encode(packet, function(encodedPackets) {
55 var decoder = new parser.Decoder();
56 decoder.on('decoded', function(decodedPacket) {
57 // decodedPacket.type == parser.BINARY_EVENT
58 // Buffer.isBuffer(decodedPacket.data.i) == true
59 // Buffer.isBuffer(decodedPacket.data.j) == true
60 // decodedPacket.id == 15
63 for (var i = 0; i < encodedPackets.length; i++) {
64 decoder.add(encodedPackets[i]);
68 See the test suite for more examples of how socket.io-parser is used.