Easy JSON over sockets or streams.
Writing raw text over streams or WebSockets is typically not enough to support robust communications between client and server. Let's assume that you need to pass different types of data such as configuration information and messages. How would you do this? How would the client know which is which?
One simple solution is to develop a JSON protocol to handle this:
"type": "config""contents": /* config info */
"type": "message""contents": "Your son shall be named Chalupa Batman!"
If your JSON objects are too large, you can't simply
stringify() it and assume that the full message will be received in one data read on the client. You must use a technique called framing to solve this problem. This is a fancy way of saying that you need to delimit your data so that the receiving end can tell the beginning and the end.
jsock does this for you automatically. Now you can build robust communication protocols within Node.js and within the browser.
jsock = JSON over sockets.
npm install --save jsock
component install jprichardson/jsock
bower install jsock
All you need to worry about are two things:
jsockfunction either a readable, writeable, or duplex (both) stream. This means that the input object must have either
write()function or a
on('data', data)function, or both. That's it. Easy.
This simulates a server and a client connecting.
var jsock = require'jsock'net = require'net'var PORT = 45643var server = netcreateServervar client = jsockclientclienton'data'console.logdatatype //messageconsole.logdatacontents //Hello server!clientwritetype: 'ack' contents: 'Welcome client!'serverlistenPORT//simulate later in timesetTimeoutvar client = jsocknetcreateConnectionPORTclienton'data'console.logdatatype //ackconsole.logdatacontents //Welcome client!clientstreamend //<--- notice accessing the original stream?servercloseclientwritetype: 'message' contents: "Hello server!"250
var express = require'express' //<--- not necessary, but here for examplehttp = require'http'shoe = require'shoe'jsock = require'jsock'var app = express/**... express config here ...**/var server = httpcreateServerappvar sock = shoevar jsockStream = jsockstream //stream is your websocket client connectingjsockStreamon'data'console.logdatatype //messageconsole.logdatamessage //hey web server!jsockStreamwritetype: 'message' content: 'hey web browser'serverlistenappget'port'console.log"Web server listening in %s on port %d" processenvNODE_ENV appget'port';sockinstallserver '/data' //<--- websocket path, name it whatever as long as it doesn't conflict with your express routes
client.js: (this should be in your browser)
var shoe = require'shoe'jsock = require'jsock'var stream = shoe'/data'var jsockStream = jsockstreamjsockStreamon'data'console.logdatatype //messageconsole.logdatamessage //hey web browserjsockStreamwritetype: 'message' content: 'hey web server!'
You can use this with plain old WebSockets as well. I may modify jsock to do this mapping for you.
This has not been tested, but something like this should work:
var origin = windowlocationoriginsplit'//'1var ws = 'ws://' + origin + '/data' //<--- path that you define on the serverwswrite = wssend //map 'send' to 'write'//create on('data') eventif event === 'data'callbackeventdatavar jsockStream = jsockws//now use jsockStream as you would
Copyright 2013, JP Richardson firstname.lastname@example.org