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

fake-web-socket-server

v1.1.2

Published

Fake Web Socket Server

Downloads

53

Readme

Fake Web Socket Server

Get a web socket server which generates fake data in 30 seconds or less.

Install the Server

To add the fake web socket server to your application, run the following command in a terminal window within your project's top-level folder (where package.json lives)

npm install -D -E fake-web-socket-server

This command will install and register the fake web socket server as a development dependency (not poluting your production releases). Also, the version number will be fixed to prevent against future upgrades from breaking your code.

Run the Server

If you running a newer version of Node.js which supports the npx command, you can run the server from the a terminal window within the project's top-level folder using the following command:

npx fake-web-socket-server

If you would like to run the server as part of you npm scripts configuration, thenm odify your package.json file by adding a new scripts entry.

{
  /* other entries */
  "scripts": {
    /* other entries */
    "fake-web-socket-server": "fake-web-socket-server"
  }
}

From a terminal window within your project's top-level folder run the following command:

npm run fake-web-socket-server

Server Options

--port, -p - set the port number, the default port is 8080

Example Code

With the JavaScript of your web application, run the following code:


// ensure the port number matches the default one or the one you specified from the command line
const webSocket = new WebSocket('ws://localhost:8080');

webSocket.addEventListener('message', message => {
  console.log(JSON.parse(message.data));
});

webSocket.addEventListener('open', () => {

  webSocket.send(JSON.stringify({
    type:'echo',
    payload: 'test',
  }));

});

Load the web page into your web browser, then open the console in the browser's development tools. The echo content should appear.

API

Each message should be given a type. The supported types are:

  • echo - echos the payload back immediately
  • counter - generates 10 numbers on a fixed interval of 500 milliseconds
  • random_number - generates 10 random numbers (0 to 1) on a fixed interval of 500 milliseconds

Each type supports additional options:

Echo Type

Option(s):

  • delay - the delay (in milliseconds) before sending the echoed payload from the server

Code Example:

webSocket.send(JSON.stringify({
  type:'echo',
  payload: 'test',
  delay: 1000, // delay 1 second (1000 milliseconds)
}));

Counter Type

Option(s):

  • interval - the interval (in milliseconds) between each number sent from the server
  • take - the number of numbers to send

Code Example:

webSocket.send(JSON.stringify({
  type:'counter',
  interval: 2000, // send a number every two seconds (2000 milliseconds)
  take: 100, // send 100 numbers
}));

Random Number Type

Option(s):

  • interval - the interval (in milliseconds) between each random number sent from the server
  • take - the number of numbers to send

Code Example:

webSocket.send(JSON.stringify({
  type:'random_number',
  interval: 2000, // send a random number every two seconds (2000 milliseconds)
  take: 100, // send 100 numbers
}));