npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsock

v0.0.3

Published

Easy JSON over sockets or streams.

Downloads

9

Readme

jsock

JavaScript component for easy JSON handling over sockets or streams. Works in Node.js or the browser.

Why?

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:

config data:

{
  "type": "config",
  "contents": {/* config info */}
}

message data:

{
  "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.

Name

jsock = JSON over sockets.

Install

Node.js/Browserify

npm install --save jsock

Component

component install jprichardson/jsock

Bower

bower install jsock

Script

<script src="/path/to/jsock.js"></script>

Usage

All you need to worry about are two things:

  1. The jsock function.
  2. The only input to the jsock function 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.

Misc:

  • If you want to access the original input stream object, access the stream property.
  • If you want to modify the delimiter, access the jsock.DELIM property.

Examples

Node.js

This simulates a server and a client connecting.

var jsock = require('jsock')
  , net = require('net')

var PORT = 45643

var server = net.createServer(function(client) {
  var client = jsock(client)
  client.on('data', function(data) {
    console.log(data.type) //message
    console.log(data.contents) //Hello server!
    client.write({type: 'ack', contents: 'Welcome client!'})
  })
})

server.listen(PORT)

//simulate later in time
setTimeout(function() {
  var client = jsock(net.createConnection(PORT))
  client.on('data', function(data) {
    console.log(data.type) //ack
    console.log(data.contents) //Welcome client!

    client.stream.end() //<--- notice accessing the original stream?
    server.close()
  })
  client.write({type: 'message', contents: "Hello server!"})
},250)

Node.js / Browserify

One of the easiest ways to communicate between the browser and Node.js is to use Websockets. Specifically, shoe. Shoe is a wrapper around sockjs that makes the Websockets more Node.js stream like.

server.js:

var express = require('express') //<--- not necessary, but here for example
  , http = require('http')
  , shoe = require('shoe')
  , jsock = require('jsock')

var app = express()

/**
  ... express config here ...
**/

var server = http.createServer(app)

var sock = shoe(function(stream) {
  var jsockStream = jsock(stream) //stream is your websocket client connecting

  jsockStream.on('data', function(data) {
    console.log(data.type) //message
    console.log(data.message) //hey web server!
    jsockStream.write({type: 'message', content: 'hey web browser'})
  })
})

server.listen(app.get('port'), function(){
  console.log("Web server listening in %s on port %d", process.env.NODE_ENV, app.get('port'));
})

sock.install(server, '/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 = jsock(stream)

jsockStream.on('data', function(data) {
  console.log(data.type) //message
  console.log(data.message) //hey web browser
})
jsockStream.write({type: 'message', content: 'hey web server!'})

Node.js / Browser WebSockets

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 = window.location.origin.split('//')[1]
var ws = new WebSocket('ws://' + origin + '/data') //<--- path that you define on the server
ws.write = ws.send //map 'send' to 'write'

//create on('data') event
ws.on = function(event, callback) {
  if (event === 'data') {
    ws.onmessage = function(event) {
      callback(event.data)
    }
  }
}

var jsockStream = jsock(ws) 

//now use jsockStream as you would

License

(MIT License)

Copyright 2013, JP Richardson [email protected]