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

ws-test-server

v1.0.2

Published

starts a basic WebSocket server and allows line-by-line input from the stdin stream where each line must be a complete JSON object formatted so JSON.parse() can understand it. each line is sent as JSON.stringify() data to the front-end through the socket.

Downloads

1

Readme

Description

ws-test-server is an easy-to-install WebSocket server with a configurable port that takes line-by-line JSON input from the server's STDIN stream, sending each line through the connected socket. Client must initiate WebSocket Handshake before the prompt to enter JSON data will appear on the back-end console. This is usually done through the standard WebSocket API documented here. Once connected, the ws-test-server will appear with the prompt:

Enter JSON:

Each line is treated as a separate (and complete) JSON object. JSON parsing is performed on the input before it is converted back to a string, ensuring proper input formatting. For instance, a line like the following could be entered (press enter to send the line):

Enter JSON: { "house" : { "squareFeet" : 3000, "stories" : 3, "bedrooms" : 4, "bathrooms" : 2.5 }

Once sent, remember to use JSON.parse() on your front-end, since WebSockets only send string data by default and objects must be deserialized.

Piping data to ws-test-server

It is also very easy to pipe file input to a server-side app running ws-test-server. Consider an example app.js which runs the ws-test-server program loop (by calling wsTestBegin()). The bash shell command to pipe an input file (where each line is a completed JSON object with quotes ("") around each property name) would look like the following:

cat file.txt | node app.js

Read on for installation instructions and examples of basic usage!

Installation

npm install ws-test-server

Basic usage

There are two main parts to the basic usage of ws-test-server: back-end and front-end. The server itself runs on the back-end with a program loop to retrieve JSON data line-by-line, while the front-end code must intiiate the WebSocket connection to the back-end WebSocket Server for it to work.

Basic usage

Back-end (Node JS)

Import the function wsTestBegin from 'ws-test-server'

import wsTestBegin from 'ws-test-server'

Three ways to initialize the server-side input loop:

  1. Specify the port when calling wsTestBegin:

    wsTestBegin(6061)
       
  2. (React or Vanilla JS) Set environment variable WS_TEST_PORT=[your port]. Access this value via process.env.WS_TEST_PORT in both front and back-end code to share the common port number:

    // .env in your local environment file
    WS_TEST_PORT=6061
  3. Simply call wsTestBegin() with no parameters. The module will default to using port 7071:

     wsTestBegin() 

Front-end

React JS

// use the WebSocket API to initialize a connection to the server
// started by ws-test-server module using WS_TEST_PORT env variable
const ws = new WebSocket(`ws://yoursite.com/${process.env.WS_TEST_PORT}`)

Vanilla JS

// use the WebSocket API, but since the environment variable is
// unavailable, provide the test port manually / retrieved via
// API call
const ws = new WebSocket(`ws://yoursite.com/${known_port_value}`)

Then just use your WebSocket (React & Vanilla JS)

ws.addEventListener('open', (e) => {
    console.log(`socket opened on port ${process.env.WS_TEST_PORT}`)
})

ws.addEventListener('message', (e) => {
    console.log('socket received data: %s', e.data))
})

ws.addEventListener('close', (e) => {
    console.log('socket closed')
})

Feel free to contact me if you have questions. Happy testing!

Roy McClanahan