Skip to content

Commit 1186445

Browse files
docs: update README
1 parent bf21d49 commit 1186445

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

Diff for: README.md

+30-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
An alternative to the default [socket.io-parser](https://github.com/socketio/socket.io-parser), encoding and decoding packets with [msgpack](http://msgpack.org/).
55

6-
With that parser, the browser build will be a bit heavier (an additional 7.5 KB minified, 3.0 KB gzipped), but each message will be smaller (sent as binary).
6+
With that parser, the browser build will be a bit heavier (an additional 7.5 KB minified, 3.0 KB gzipped), but each message will be smaller (sent as binary).
7+
8+
Please note that you MUST use the parser on both sides (server & client).
9+
10+
See also:
11+
12+
- the default parser: https://github.com/socketio/socket.io-parser
13+
- a parser based on JSON.stringify/parse: https://github.com/darrachequesne/socket.io-json-parser
714

815
## Usage
916

@@ -12,15 +19,33 @@ const io = require('socket.io');
1219
const ioc = require('socket.io-client');
1320
const customParser = require('socket.io-msgpack-parser');
1421

15-
let server = io(PORT, {
22+
const server = io(PORT, {
1623
parser: customParser
1724
});
1825

19-
let client = ioc('ws://localhost:' + PORT, {
26+
const socket = ioc('ws://localhost:' + PORT, {
2027
parser: customParser
2128
});
2229

23-
client.on('connect', () => {
24-
client.emit('hello');
30+
socket.on('connect', () => {
31+
socket.emit('hello');
2532
});
2633
```
34+
35+
## Format
36+
37+
`socket.emit('hello', 'you')` will create the following packet:
38+
39+
```json
40+
{
41+
"type": 2,
42+
"nsp": "/",
43+
"data": ["hello", "you"]
44+
}
45+
```
46+
47+
which will be encoded by the parser as:
48+
49+
`<Buffer 83 a4 74 79 70 65 02 a3 6e 73 70 a1 2f a4 64 61 74 61 92 a5 68 65 6c 6c 6f a3 79 6f 75>`
50+
51+
More information about the exchange protocol can be found [here](https://github.com/socketio/socket.io-protocol).

0 commit comments

Comments
 (0)