Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/socketio/socket.io-json-parser
socket.io parser based on JSON.stringify / JSON.parse
https://github.com/socketio/socket.io-json-parser
json socket-io socket-io-parser
Last synced: 4 months ago
JSON representation
socket.io parser based on JSON.stringify / JSON.parse
- Host: GitHub
- URL: https://github.com/socketio/socket.io-json-parser
- Owner: socketio
- License: mit
- Created: 2017-04-27T22:36:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-09-06T17:59:52.000Z (over 1 year ago)
- Last Synced: 2024-04-14T12:01:44.568Z (9 months ago)
- Topics: json, socket-io, socket-io-parser
- Language: JavaScript
- Size: 232 KB
- Stars: 12
- Watchers: 2
- Forks: 8
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE
Awesome Lists containing this project
README
# socket.io-json-parser
An alternative to the default [socket.io-parser](https://github.com/socketio/socket.io-parser), encoding and decoding packets with `JSON.parse / stringify`.
With that parser, binary data (ArrayBuffer / Buffer / Blob / File) is not supported.
Please note that you MUST use the parser on both sides (server & client).
See also:
- the default parser: https://github.com/socketio/socket.io-parser
- a parser based on msgpack: https://github.com/darrachequesne/socket.io-msgpack-parser## Usage
```js
const io = require('socket.io');
const ioc = require('socket.io-client');
const customParser = require('socket.io-json-parser');const server = io(PORT, {
parser: customParser
});const socket = ioc('ws://localhost:' + PORT, {
parser: customParser
});socket.on('connect', () => {
socket.emit('hello');
});
```## Format
`socket.emit('hello', 'you')` will create the following packet:
```json
{
"type": 2,
"nsp": "/",
"data": ["hello", "you"]
}
```which will be encoded by the parser as:
`{"type":2,"nsp":"/","data":["hello","you"]}`
More information about the exchange protocol can be found [here](https://github.com/socketio/socket.io-protocol).