{"_id":"json-socket","_rev":"949771","name":"json-socket","description":"An easy way to send JSON over TCP using sockets.","dist-tags":{"latest":"0.3.0"},"maintainers":[{"name":"samuelbolduc","email":"sam@samuelbolduc.info"},{"name":"sebastianseilund","email":"sebastian@seilund.com"}],"time":{"modified":"2021-09-23T02:22:44.000Z","created":"2012-12-01T06:12:07.319Z","0.3.0":"2017-06-12T15:55:54.491Z","0.2.1":"2016-12-15T15:58:55.665Z","0.2.0":"2016-09-19T17:20:57.926Z","0.1.2":"2012-12-02T23:20:53.804Z","0.1.1":"2012-12-01T06:12:07.319Z"},"users":{"yanislavgalyov":true,"hyde2able":true,"chimit":true,"larnera":true,"robba.jt":true,"braviel":true,"chzhewl":true},"author":{"name":"Sebastian Seilund"},"repository":{"type":"git","url":"git+ssh://git@github.com/sebastianseilund/node-json-socket.git"},"versions":{"0.3.0":{"name":"json-socket","description":"An easy way to send JSON over TCP using sockets.","main":"./lib/json-socket","author":{"name":"Sebastian Seilund"},"version":"0.3.0","repository":{"type":"git","url":"git+ssh://git@github.com/sebastianseilund/node-json-socket.git"},"bugs":{"url":"http://github.com/sebastianseilund/node-json-socket/issues"},"maintainers":[{"name":"samuelbolduc","email":"sam@samuelbolduc.info"},{"name":"sebastianseilund","email":"sebastian@seilund.com"}],"licenses":[{"type":"MIT","url":"http://github.com/sebastianseilund/node-json-socket/raw/master/LICENSE"}],"devDependencies":{"async":"^2.0.1","mocha":"^3.0.2"},"scripts":{"test":"mocha --recursive test"},"gitHead":"108a9664ec696f83ea2fd36c0bbad852c38e19cb","homepage":"https://github.com/sebastianseilund/node-json-socket#readme","_id":"json-socket@0.3.0","_npmVersion":"5.0.0","_nodeVersion":"8.0.0","_npmUser":{"name":"samuelbolduc","email":"sam@samuelbolduc.info"},"dist":{"shasum":"f4b953c685bb8e8bd0b72438f5208d9a0799ae07","size":5349,"noattachment":false,"key":"/json-socket/-/json-socket-0.3.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/json-socket/download/json-socket-0.3.0.tgz"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/json-socket-0.3.0.tgz_1497282953457_0.8209022406954318"},"directories":{},"publish_time":1497282954491,"_hasShrinkwrap":false,"_cnpm_publish_time":1497282954491},"0.2.1":{"name":"json-socket","description":"An easy way to send JSON over TCP using sockets.","main":"./lib/json-socket","author":{"name":"Sebastian Seilund"},"version":"0.2.1","repository":{"type":"git","url":"git+ssh://git@github.com/sebastianseilund/node-json-socket.git"},"bugs":{"url":"http://github.com/sebastianseilund/node-json-socket/issues"},"maintainers":[{"name":"samuelbolduc","email":"sam@samuelbolduc.info"},{"name":"sebastianseilund","email":"sebastian@seilund.com"}],"licenses":[{"type":"MIT","url":"http://github.com/sebastianseilund/node-json-socket/raw/master/LICENSE"}],"devDependencies":{"async":"^2.0.1","mocha":"^3.0.2"},"scripts":{"test":"mocha --recursive test"},"gitHead":"5884ebdead997ee1bd79f0f432838172579e45d9","homepage":"https://github.com/sebastianseilund/node-json-socket#readme","_id":"json-socket@0.2.1","_shasum":"26e7ed8cc131f17aa0136c32068f473b5dbefb22","_from":".","_npmVersion":"3.10.8","_nodeVersion":"7.0.0","_npmUser":{"name":"samuelbolduc","email":"sam@samuelbolduc.info"},"dist":{"shasum":"26e7ed8cc131f17aa0136c32068f473b5dbefb22","size":5271,"noattachment":false,"key":"/json-socket/-/json-socket-0.2.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/json-socket/download/json-socket-0.2.1.tgz"},"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/json-socket-0.2.1.tgz_1481817534940_0.8105558725073934"},"directories":{},"publish_time":1481817535665,"_cnpm_publish_time":1481817535665,"_hasShrinkwrap":false},"0.2.0":{"name":"json-socket","description":"An easy way to send JSON over TCP using sockets.","main":"./lib/json-socket","author":{"name":"Sebastian Seilund"},"version":"0.2.0","repository":{"type":"git","url":"git+ssh://git@github.com/sebastianseilund/node-json-socket.git"},"bugs":{"url":"http://github.com/sebastianseilund/node-json-socket/issues"},"maintainers":[{"name":"samuelbolduc","email":"sam@samuelbolduc.info"},{"name":"sebastianseilund","email":"sebastian@seilund.com"}],"licenses":[{"type":"MIT","url":"http://github.com/sebastianseilund/node-json-socket/raw/master/LICENSE"}],"devDependencies":{"async":"^2.0.1","mocha":"^3.0.2"},"scripts":{"test":"mocha --recursive test"},"gitHead":"314de9849bad8d9c5c27ea8c8b9f4e86f2a6addf","homepage":"https://github.com/sebastianseilund/node-json-socket#readme","_id":"json-socket@0.2.0","_shasum":"d01e2bfa7cb212647b9eea7860a283ad402fa077","_from":".","_npmVersion":"3.3.12","_nodeVersion":"5.3.0","_npmUser":{"name":"sebastianseilund","email":"sebastian@seilund.com"},"dist":{"shasum":"d01e2bfa7cb212647b9eea7860a283ad402fa077","size":5162,"noattachment":false,"key":"/json-socket/-/json-socket-0.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/json-socket/download/json-socket-0.2.0.tgz"},"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/json-socket-0.2.0.tgz_1474305657678_0.04783686972223222"},"directories":{},"publish_time":1474305657926,"_cnpm_publish_time":1474305657926,"_hasShrinkwrap":false},"0.1.2":{"name":"json-socket","description":"An easy way to send JSON over TCP using sockets.","main":"./lib/json-socket","author":{"name":"Sebastian Seilund"},"version":"0.1.2","repository":{"type":"git","url":"http://github.com/sebastianseilund/node-json-socket.git"},"bugs":{"url":"http://github.com/sebastianseilund/node-json-socket/issues"},"maintainers":[{"name":"samuelbolduc","email":"sam@samuelbolduc.info"},{"name":"sebastianseilund","email":"sebastian@seilund.com"}],"licenses":[{"type":"MIT","url":"http://github.com/sebastianseilund/node-json-socket/raw/master/LICENSE"}],"devDependencies":{"mocha":"*"},"dependencies":{"async":"0.1.x"},"scripts":{"test":"./node_modules/.bin/mocha $(shell find test -name '*.test.js')"},"_id":"json-socket@0.1.2","dist":{"shasum":"d039e0e0deb636268bd0ba7c7c036d4d63998435","size":10042,"noattachment":false,"key":"/json-socket/-/json-socket-0.1.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/json-socket/download/json-socket-0.1.2.tgz"},"_npmVersion":"1.1.62","_npmUser":{"name":"sebastianseilund","email":"sebastian@seilund.com"},"directories":{},"publish_time":1354490453804,"_cnpm_publish_time":1354490453804,"_hasShrinkwrap":false},"0.1.1":{"name":"json-socket","description":"An easy way to send JSON over TCP using sockets.","main":"./lib/json-socket","author":{"name":"Sebastian Seilund"},"version":"0.1.1","repository":{"type":"git","url":"http://github.com/sebastianseilund/node-json-socket.git"},"bugs":{"url":"http://github.com/sebastianseilund/node-json-socket/issues"},"maintainers":[{"name":"samuelbolduc","email":"sam@samuelbolduc.info"},{"name":"sebastianseilund","email":"sebastian@seilund.com"}],"licenses":[{"type":"MIT","url":"http://github.com/sebastianseilund/node-json-socket/raw/master/LICENSE"}],"devDependencies":{"mocha":"*"},"dependencies":{"async":"0.1.x"},"scripts":{"test":"./node_modules/.bin/mocha $(shell find test -name '*.test.js')"},"_id":"json-socket@0.1.1","dist":{"shasum":"5555c627de709f919408154958bcf18aed9984d2","size":7222,"noattachment":false,"key":"/json-socket/-/json-socket-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/json-socket/download/json-socket-0.1.1.tgz"},"_npmVersion":"1.1.62","_npmUser":{"name":"sebastianseilund","email":"sebastian@seilund.com"},"directories":{},"publish_time":1354342327319,"_cnpm_publish_time":1354342327319,"_hasShrinkwrap":false}},"readme":"# JsonSocket [![Build Status](https://secure.travis-ci.org/sebastianseilund/node-json-socket.png?branch=master)](https://travis-ci.org/sebastianseilund/node-json-socket)\n\n`JsonSocket` is a very easy way to send JSON over a socket using either TCP or UNIX sockets. `JsonSocket` uses a [decorator pattern](http://en.wikipedia.org/wiki/Decorator_pattern)\nto expose extra methods and events on instances of [`net.Socket`](http://nodejs.org/api/net.html#net_class_net_socket).\nThese methods and events makes it straightforward to send bidirectional JSON messages of any kind and size.\n\n\n## Installation\n\nYou can install `JsonSocket` using Node Package Manager (npm): \n\n```\nnpm install json-socket\n```\n\nOr add it to your `package.json` file, like this:\n\n```json\n{\n    ...\n    \"dependencies\": {\n        \"json-socket\": \"*\"\n    }\n    ...\n}\n```\n\nAnd then run:\n\n```\nnpm install\n```\n\n## Usage\n\n`JsonSocket` relies on Node.js' built-in [`net`](http://nodejs.org/api/net.html) package. Everytime you have an instance\nof `net.Socket` you simply wrap in an instance of `JsonSocket`.\n\n### Simple client/server example\n\nHere is a simple example where a client can connect to the server and send two numbers (a and b) that the server multiplies\nand sends back the result.\n\n#### Server\n\n```javascript\nvar net = require('net'),\n    JsonSocket = require('json-socket');\n\nvar port = 9838;\nvar server = net.createServer();\nserver.listen(port);\nserver.on('connection', function(socket) { //This is a standard net.Socket\n    socket = new JsonSocket(socket); //Now we've decorated the net.Socket to be a JsonSocket\n    socket.on('message', function(message) {\n        var result = message.a + message.b;\n        socket.sendEndMessage({result: result});\n    });\n});\n```\n\n### Client\n\n```javascript\nvar net = require('net'),\n    JsonSocket = require('json-socket');\n\nvar port = 9838; //The same port that the server is listening on\nvar host = '127.0.0.1';\nvar socket = new JsonSocket(new net.Socket()); //Decorate a standard net.Socket with JsonSocket\nsocket.connect(port, host);\nsocket.on('connect', function() { //Don't send until we're connected\n    socket.sendMessage({a: 5, b: 7});\n    socket.on('message', function(message) {\n        console.log('The result is: '+message.result);\n    });\n});\n```\n\n---------------------------------------\n\n### Streaming example\n\nHere is an example of a server that can stream square numbers. A client can connect and send a `start` command which should\nalso include a `beginAt` value, i.e. which number should be squared first. Then the server streams a square every second,\nuntil the client sends a `stop` command.\n\n#### Server\n\n```javascript\nvar net = require('net'),\n    JsonSocket = require('json-socket');\n\nvar port = 9838;\nvar server = net.createServer();\nserver.listen(port);\nserver.on('connection', function(socket) {\n    socket = new JsonSocket(socket);\n    var n;\n    var isRunning = false;\n    var streatTimeout;\n    socket.on('message', function(message) {\n        if (message.command == 'start') {\n            if (!isRunning) {\n                n = message.beginAt || 1;\n                isRunning = true;\n                streamInterval = setInterval(function() {\n                    socket.sendMessage(n * n);\n                    n++;\n                }, 1000);\n            }\n        } else if (message.command == 'stop') {\n            if (isRunning) {\n                isRunning = false;\n                clearInterval(streamInterval);\n            }\n        }\n    });\n});\n```\n\n### Client\n\n```javascript\nvar net = require('net'),\n    JsonSocket = require('json-socket');\n\nvar port = 9838; //The same port that the server is listening on\nvar host = '127.0.0.1';\nvar socket = new JsonSocket(new net.Socket()); //Decorate a standard net.Socket with JsonSocket\nsocket.connect(port, host);\nsocket.on('connect', function() { //Don't send until we're connected\n    socket.sendMessage({command: 'start', beginAt: 10});\n    socket.on('message', function(square) {\n        console.log(square);\n        if (square > 200) {\n            socket.sendMessage({command: 'stop'});\n        }\n    });\n});\n```\n\nWill output:\n\n```\n100\n121\n144\n169\n196\n225\n```\n\n---------------------------------------\n\n### Sending a single message\n\nJsonSocket has two helper methods that are useful if you just need to send a single message and you don't need the socket\nto stay open.\n\nThe second method is `JsonSocket.sendSingleMessage`. It sends a single message and closes the connection instantly.\nUse this if you need to send a server a message, but you don't need any response.\n\n```javascript\nvar JsonSocket = require('json-socket');\n\nJsonSocket.sendSingleMessage(port, host, {type: 'ping'}, function(err) {\n    if (err) {\n        //Something went wrong\n        throw err;\n    }\n    console.log('Pinged '+host+' on port '+port);\n});\n````\n\nThe second method is `JsonSocket.sendSingleMessageAndReceive`. It sends a single message, waits for a single response\nmessage from the server and closes the connection right after.\nUse this if you need to send a server a message, and get a response, but you don't need the connection to stay open.\n\n```javascript\nvar JsonSocket = require('json-socket');\n\nJsonSocket.sendSingleMessageAndReceive(port, host, {type: 'ping'}, function(err, message) {\n    if (err) {\n        //Something went wrong\n        throw err;\n    }\n    console.log('Server said: '+message.type); //E.g. pong\n});\n````\n\n## Message formats\n\nThe messages can be any Javascript object that can be converted to JSON:\n\n- Strings\n- Numbers\n- Booleans\n- Objects\n- Arrays\n- Anything!\n\n\n\n## Extra methods exposed on JsonSocket instances\n\nSince `JsonSocket` is a decorator of `net.Socket`, it supports all methods that `net.Socket` supports.\n\nBesides that the following methods and events are also available on `JsonSocket` instances.\n\n### JsonSocket.sendSingleMessage(port, host, message, callback)\n\nSends a single message anc close the connection instantly.\n\n__Arguments__\n\n- port - Port to send the message to.\n- most - Host to send the message to.\n- message - The message to send.\n- callback(err) - Will be called when the message has been sent.\n\n---------------------------------------\n\n### JsonSocket.sendSingleMessageAndReceive(port, host, message, callback)\n\nSends a single message to `host`:`port`, waits for a response message, and closes the connection right after.\nAs soon as the response message is received the `callback` function is invoked (if given) with the response message\nas the second argument.\n\n__Arguments__\n\n- port - Port to send the message to.\n- most - Host to send the message to.\n- message - The message to send.\n- callback(err, message) - Will be called when the response message has been received. The response message is given as the second argument.\n\n---------------------------------------\n\n### socket.sendMessage(message, callback)\n\nSends a JSON a message over the socket.\n\n__Arguments__\n\n- message - The message to send.\n- callback(err) - Will be called after the message has been sent.\n\n---------------------------------------\n\n### socket.sendEndMessage(message, callback)\n\nSame as `socket.sendMessage`, except that the socket is closed right after the message has been sent using \n[`net.end()`](http://nodejs.org/api/net.html#net_socket_end_data_encoding).\n\nNo more messages can be sent from either the server or client through this socket.\n\n---------------------------------------\n\n### socket.sendError(err, callback)\n\nConvenience method for sending an error as a message. \n\n__Arguments__\n\n- err - An Error object that should be formatted as a message.\n- callback(err) - Will be called after the message has been sent.\n\n__Example__\n\n```javascript\nsocket.sendError(new Error('Something went wrong!');\n```\n\nWill send a message of this JSON format:\n\n```json\n{\n    \"success\": false,\n    \"error\": \"Something went wrong\"\n}\n```\n\n---------------------------------------\n\n### socket.sendEndError(err, callback)\n\nSame as `socket.sendError`, except that the socket is closed right after the message has been sent using \n[`net.end()`](http://nodejs.org/api/net.html#net_socket_end_data_encoding).\n\nNo more messages can be sent from either the server or client through this socket.\n\n---------------------------------------\n\n### socket.isClosed()\n\nReturns true if either the server or the client has closed the connection. Returns false otherwise.\n\n---------------------------------------\n\n### Event: 'message'\n\n- message - the message received from the other side.\n\nEmitted when a complete message has been received.\n\n\n## How the protocol works\n\nThe `JsonSocket` protocol works by `JSON.stringify`'ing the message and prefixing it with a content length and a content length delimiter (#).\n\nExample:\n\n```javascript\nsocket.sendMessage({\n    type: \"ping\"\n});\n```\n\nWill send a message that looks like this:\n\n```\n15#{\"type\":\"ping\"}\n```\n\nThis mechanism ensures that messages that are chunked will be parsed correctly.\n\n\n##Todo\n\n- `socket.request()` method, and `request` event. Makes it easier to send multiple requests with callbacks.\n\n\n\n\n\n\n\n","_attachments":{},"homepage":"https://github.com/sebastianseilund/node-json-socket#readme","bugs":{"url":"http://github.com/sebastianseilund/node-json-socket/issues"}}