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

dataportal

v0.0.3

Published

Share JSON across websockets

Downloads

10

Readme

alt text

Share JSON across websockets

Installation

npm install dataportal

Getting started

Create a dataportal server in node, eg:

require('dataportal')();

And run it with node - it should now be running on port 32827 - to use the client side:

<script type="text/javascript" src="node_modules/dataportal/build/dataportal.js?url=http://local.mac:32827/dataPortal"></script>
<script>
var obj = {"hello": "world"};

//	Create a portal for this topic
mp = dataPortal(obj, "myobject", {
	onready: function(portal){
		//	Modify our object and publish it
		obj.time = (new Date()).getTime();
		portal.subscribe(function(value){console.log(value);});
		portal.publish(obj);
	}
});
</script>

</body>
</html>

Note: Change "http://local.mac:32827/dataPortal" to the IP address or URL of where you're running your dataportal server. This example should log out a message each time the object is published for the "myobject" topic - it should show the same value on the last console.log line in each browser.

Um, why?

Curiosity - I've seen many other implementations - including what they do in MeteorJS - they all seem unnecessarily complex for something as simple as syncing JSON across a websocket, so this is just an experiment to see if it can be done a little simpler. Obviously this means we haven't implemented any protocols (such as DDP), but rather just allow object to be synchronized, using https://github.com/benjamine/jsondiffpatch for efficiency. As I said, this is an experiment, and so is not meant for production code.

Usage

You create an object you want to share, and then a data portal with a topic:

dataPortal(Object, Topic, [Arguments]);
  • Object {object}: The object you want to track
  • Topic {string}: The name of the topic - make sure this is unique per data portal server
  • Arguments {object}: Optional arguments

Arguments include:

  • onready {function(portal)}: Callback function for when the portal has been initialised and is connected - passes back the portal object
  • onpatch {function(object, diff)}: Manually handle the patching process - the difference has what the new object changes are as per the diffs used in https://github.com/benjamine/jsondiffpatch.
  • onclose {function(portal)}: Callback for when the server closes the connection, you can try to re-establish the connection by reloading the page, or the portal.

Portal object

The portal object controls how your data is shared with the server, you

  • publish {function(newObjectValue)}: If you change the object, use this to publish the new object value
  • subscribe {function(func{function(object)})}: Callback function that receives the object when it is changed
  • close {function(func{function(object)})}: Subscribe a callback for when the server closes the connection, you can try to re-establish the connection by reloading the page, or the portal.

Examples:

See the examples folder in this repository.

Note: The examples included with Dataportal have the URL of the server set to "http://local.mac:32827/dataPortal", be sure to adjust that to it uses the IP address or URL of where you're running your dataportal server, for when you test on separate machines.