Kind: Class
Source: packages/microservices/helpers/json-socket.ts
Part of: Microservices
JsonSocket wraps a TCP socket with length-prefixed JSON message framing for the microservices transport layer. It serializes outgoing payloads in handleSend() and incrementally buffers, reconstructs, and parses incoming TCP chunks in handleData() before delivering complete messages to registered listeners.
Extends: TcpSocket
Methods
| Method | Signature | Returns |
|---|---|---|
handleSend | handleSend(message: any, callback: (err?: any) => void) | void |
handleData | `handleData(dataRaw: Buffer | string)` |
Diagram
mermaidgraph LR A[Application message] --> B[JsonSocket.handleSend] B --> C[Serialize JSON and add length prefix] C --> D[TCP socket] D --> E[Incoming TCP chunks] E --> F[JsonSocket.handleData] F --> G[Buffer and resolve frame length] G --> H[Parse complete JSON message] H --> I[Message listener]
AI Coding Instructions
- Preserve the length-prefixed framing format when changing
handleSend()orhandleData(); TCP data may arrive split across multiple chunks or contain multiple messages in one chunk. - Use
handleSend()for outgoing serialization rather than writing raw JSON directly to the underlying socket. - Keep buffering logic resilient to partial payloads and reset parser state only after a complete frame has been processed.
- Treat malformed frame lengths and invalid JSON as transport-level errors; avoid silently emitting incomplete or corrupted messages.
- Prefer the inherited
send(),onMessage(),connect(), andclose()APIs at integration points instead of calling the internal handlers directly.
Was this page helpful?