{"library":"socket.io-parser","title":"Socket.IO Protocol Parser","type":"library","description":"socket.io-parser is the foundational library responsible for encoding and decoding packets for the Socket.IO protocol. It is an internal component used by both `socket.io` (server) and `socket.io-client` (browser/Node.js client) to serialize and deserialize messages, including binary data, into a transportable format compatible with the underlying Engine.IO layer. The current stable version is 4.2.6. Releases are typically aligned with major `socket.io` ecosystem updates and critical security patches, such as recent fixes for CVEs. While primarily an internal dependency, it can be used directly for advanced use cases like implementing custom Socket.IO parsers or debugging the protocol, offering a low-level interface to the Socket.IO communication structure.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install socket.io-parser"],"cli":null},"imports":["import { Encoder } from 'socket.io-parser'","import { Decoder } from 'socket.io-parser'","import { PacketType } from 'socket.io-parser'"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/socketio/socket.io","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/socket.io-parser","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import { Encoder, Decoder, PacketType } from 'socket.io-parser';\nimport { EventEmitter } from 'events';\n\n// Polyfill EventEmitter for Decoder in browser contexts if needed\n// Decoder.prototype.__proto__ = EventEmitter.prototype; // Example if not using modern polyfills\n\ninterface MyPacket {\n  type: PacketType;\n  data: any;\n  id?: number;\n  nsp: string;\n}\n\nconst encoder = new Encoder();\nconst decoder = new Decoder();\n\n// Simulate an outgoing Socket.IO event packet\nconst originalPacket: MyPacket = {\n  type: PacketType.EVENT,\n  data: ['hello', { world: true, value: 123 }],\n  id: 42,\n  nsp: '/',\n};\n\nconsole.log('Original Packet:', originalPacket);\n\nencoder.encode(originalPacket, (encodedPackets: (string | Buffer)[]) => {\n  console.log('Encoded Packets:', encodedPackets);\n\n  let decodedPacket: MyPacket | undefined;\n  decoder.on('decoded', (packet: MyPacket) => {\n    decodedPacket = packet;\n    console.log('Decoded Packet:', decodedPacket);\n\n    // Verify the decoded packet\n    if (\n      decodedPacket.type === originalPacket.type &&\n      JSON.stringify(decodedPacket.data) === JSON.stringify(originalPacket.data) &&\n      decodedPacket.id === originalPacket.id &&\n      decodedPacket.nsp === originalPacket.nsp\n    ) {\n      console.log('Packet encoding and decoding successful!');\n    } else {\n      console.error('Packet mismatch after encoding/decoding.');\n    }\n  });\n\n  for (const chunk of encodedPackets) {\n    decoder.add(chunk);\n  }\n});","lang":"typescript","description":"Demonstrates how to manually encode and decode a Socket.IO event packet using `Encoder` and `Decoder` classes, including handling packet types and verifying data integrity.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}