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

djs-chain

v1.0.12

Published

Chained objects from browser to SQL database

Downloads

33

Readme

DJS-chain

This code illustrates a chain of linked objects from browsers to a single database via two servers using HTTP and WebSocket.

Main Features

This code is part of the Diffuse Javascript MOOC. It illustrates a chain of objects starting in the browser, linked to server objects and backed up in an SQL database. When an object is modified in a browser, an HTTP REST API call propagates the modification towards the corresponding server object which is persisted in an SQL database, the modification is then pushed towards the various clients of the server via WebSocket so, at the end, all browsers should eventually share the same state.

browserObject.setProperty(propName, newValue)
   ==> HTTP REST request to the server:
       ===> convert HTTP request into javascript code:
            serverObject.setProperty(propName, newValue)
            ===> convert modification into SQL: 
                 update table set propName = newValue
       <==  Broadcast updates to all browser clients via JSON and WebSocket
       {update: propName, value: newValue}

The code depends only on the sqlite3 and ws modules, it does not use elaborate framework for Web, ORM, REST, etc. but just plain standard Node.js modules: http, url and fs. This keeps code minimal, simple and disembarrassed of useless features.

Files required for servers

dbobject is a tiny ORM (Object Relational Mapping) converting rows of SQL tables into Javascript objects. Conversely, modifying the Javascript object runs appropriate SQL commands to update the database.

webapi is a tiny HTTP server that offers a REST API mapping HTTP requests into access to database objects. HTTP methods such as GET, PUT, POST and DELETE are supported.

wsapi is a tiny WebSocket server that pushes modifications of database objects to all connected clients. webapi could also have been implemented with WebSocket but this separation illustrates the different aspects of the two protocols.

Files required for clients

browserobj is a tiny library that offers objects linked to remote objects hosted on some server and accessed via an HTTP REST API. A modification to such an object is translated into an HTTP PUT request. Concurrently, the library fires a WebSocket client receiving the updates to apply.

Installation

The simplest way is to install the npm module djs-chain with

npm install djs-chain
cd node_modules/djs-chain/

If you prefer to clone the repository, then you must regenerate the webapp with the following commands:

git clone https://github.com/ChristianQueinnec/DJS-chain.git
cd DJS-chain
gulp

Demo

Start a server (by default on port 18080) with:

npm run start

If you prefer another port, you may specify it explicitly with:

export PORT=18080
cd Site
node ../src/chain.js

Note that websocket will use the next port (by default 18081).

Once the server is started, fire a browser on http://127.0.0.1:18080/. The served page shows two independent frames, you may modify objects in one frame and observe the modifications arriving in the other frame (via the server). A small documentation appears on the served page.

Miscellaneous

OpenAPI

An OpenAPI description of the HTTP REST API is available in Swagger/swagger.json. You may visualize it on the Swagger site

Heroku

Does Heroku supports two simultaneous open ports (http and ws) ?