{"id":17134704,"url":"https://github.com/robojones/socket-json-wrapper","last_synced_at":"2026-04-16T19:08:18.305Z","repository":{"id":57365084,"uuid":"106098352","full_name":"robojones/socket-json-wrapper","owner":"robojones","description":"Wrapper for socket connections that allows you to send and receive json objects.","archived":false,"fork":false,"pushed_at":"2018-04-12T21:13:08.000Z","size":42,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T22:01:45.749Z","etag":null,"topics":["json","nodejs","npm","object","socket","tcp","unix"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/socket-json-wrapper","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robojones.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-07T13:02:42.000Z","updated_at":"2018-04-12T21:13:09.000Z","dependencies_parsed_at":"2022-09-13T21:11:57.514Z","dependency_job_id":null,"html_url":"https://github.com/robojones/socket-json-wrapper","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robojones%2Fsocket-json-wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robojones%2Fsocket-json-wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robojones%2Fsocket-json-wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robojones%2Fsocket-json-wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robojones","download_url":"https://codeload.github.com/robojones/socket-json-wrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245025993,"owners_count":20549071,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","nodejs","npm","object","socket","tcp","unix"],"created_at":"2024-10-14T19:45:32.448Z","updated_at":"2026-04-16T19:08:18.272Z","avatar_url":"https://github.com/robojones.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# socket-json-wrapper\n\nWrapper for socket connections that allows you to send and receive json objects.\n\n[![Build Status](https://circleci.com/gh/robojones/socket-json-wrapper.svg?style=shield\u0026circle-token=:circle-token)](https://circleci.com/gh/robojones/socket-json-wrapper/tree/master)\n[![Test Coverage](https://codeclimate.com/github/robojones/socket-json-wrapper/badges/coverage.svg)](https://codeclimate.com/github/robojones/socket-json-wrapper/coverage)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n[![bitHound Code](https://www.bithound.io/github/robojones/socket-json-wrapper/badges/code.svg)](https://www.bithound.io/github/robojones/socket-json-wrapper)\n[![bitHound Overall Score](https://www.bithound.io/github/robojones/socket-json-wrapper/badges/score.svg)](https://www.bithound.io/github/robojones/socket-json-wrapper)\n[![bitHound Dependencies](https://www.bithound.io/github/robojones/socket-json-wrapper/badges/dependencies.svg)](https://www.bithound.io/github/robojones/socket-json-wrapper/master/dependencies/npm)\n\n## Installation\n\n```\nnpm i socket-json-wrapper\n```\n\n## Usage\n\nIn the `server.js` file:\n```javascript\nconst net = require('net')\nconst { Connection } = require('socket-json-wrapper')\n\nconst server = net.createServer(socket =\u003e {\n  const connection = new Connection(socket)\n\n  connection.on('message', data =\u003e {\n    console.log('received:', data)\n    // will log { dataFromClient: 'baz' }\n  })\n\n  // send some data to the server.\n  connection.send({ dataFromServer: 'foo' })\n})\n```\n\nIn the `client.js` file:\n\n```javascript\nconst net = require('net')\nconst { Connection } = require('socket-json-wrapper')\n\nconst socket = net.createConnection(8080)\n\nconst connection = new Connection(socket)\n\nconnection.on('message', data =\u003e {\n  console.log('received:', data)\n  // will log { dataFromServer: 'foo' }\n})\n\n// send some data to the server.\nconnection.send({ dataFromClient: 'baz' })\n```\n\n## API\n\n### connection.isDead\n\n`\u003cboolean\u003e`\n\nIs set to true if the underlying socket is not writable.\n\n### connection.socket\n\nA refference to the wrapped socket.\n\n### connection.send(data)\n\n- __data__ `\u003c*\u003e` - This may be any value that can by stringified to valid JSON.\n\nIt __returns__ a `\u003cboolean\u003e` that is true if the data was written to the socket.\n\n### connection.close()\n\nThis method calls `.end()` on the underlying socket.\n\n## Typescript usage\n\nIn typescript you can define types that can be sent or received.\nYou can do so by creating an interface and by passing it to the Connection when you are creating it.\n\n```typescript\nimport { createConnection } from 'net'\nimport { Connection } from 'socket-json-wrapper'\n\nconst socket = createConnection(8080)\n\ninterface Sendable {\n  dataFromClient: string\n}\n\ninterface Receivable {\n  dataFromServer: string\n}\n\n// Pass interfaces that represent the messages that can be sent or received so you can get completions.\nconst connection = new Connection\u003cSendable, Receivable\u003e(socket)\n\nconnection.on('message', data =\u003e {\n  // Typescript will automatically know that the data parameter has a dataFromServer property.\n  console.log('received:', data)\n  // will log { dataFromServer: 'foo' }\n})\n\n// send some data to the server.\nconnection.send({ dataFromClient: 'baz' })\n// Typescript will automatically detect if your message does not match the sendable type defined in the interface above.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobojones%2Fsocket-json-wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobojones%2Fsocket-json-wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobojones%2Fsocket-json-wrapper/lists"}