Giter Site home page Giter Site logo

chaosocket's Introduction

Build Status npm version πŸ’£ ⚑ πŸ’₯ πŸ”₯ 😈

Mock WebSockets and create chaos DEMO

The motivation of this project is nothing but being able to receive predefined socket messages. Why this? usually when integrating and testing web sockets in your app it happen that you have no control over the messages you are getting. This leads to slow iterations, like having to test you app against a real source of messages (production) or not doing it at all...

Chaosocket aims to provide you a set of tools to test this sort of behaviours, also it respects the WebSocket interface, making this transparent to you, this means that you don't have to modify your code at all when you introduce the library in your app. So it's perfect for simple scenarios like what happen when the user name is longer than 50 chars? to complex ones in which you want to stress your UI with 20 messages per second.

Defining chaos boundaries πŸ“

Chaos registration

const chaos = require('chaosocket');

chaos.register(() => {
  return {
    type: 'connection',
    user: {
      name: 'Hector'
    }
  };
});

chaos.listen();

Socket usage

const socket = new WebSocket('ws://0.0.0.0:8080');

socket.onmessage = function(e) {
  const msg = JSON.parse(e.data);

  console.log(msg.type, msg.user);
};

Chaosocket will call your message callback by time to time depending in some factors with the result of your registration.

Frequency

Sometimes you want to receive more events from a specific type, you achieve that by specifying the frequency of the registration. Default value is medium and the available ones are low, medium and high. Based on it chaosocket will emit events more or less often.

chaos.register(() => {
  return {
    type: 'connection'
  };
}, 'low');

chaos.register(() => {
  return {
    type: 'typing'
  };
}, 'medium');

chaos.register(() => {
  return {
    type: 'message'
  };
}, 'high');

Delay

This will change the default delay (2000ms) to 500ms forcing the app to handle x4 more events.

chaos.listen({
  delay: 500
});

Stop chaos β›”

Tired of chaos? just close it and the socket will stop to recieve fake messages.

chaos.close();

##Β Using Faker πŸ‘» Chaosocket comes with Faker, a library to generate fake and random data easily, it's passed to the register method.

chaos.register((faker) => {
  return {
    create_at: faker.date.recent(),
    event_name: faker.random.arrayElement(['connection', 'typing', 'message']),
    first_name: faker.name.firstName(),
    last_name: faker.name.lastName(),
    bio: faker.lorem.text()
  };
});

You can find all Faker methods here https://github.com/marak/Faker.js/#api-methods

Demo πŸš€

Just a simple chat using the library

Installation πŸ”§

$ npm i chaosocket -D

Dependencies πŸ™‡

##Β Author :person_with_blond_hair:

Hector Zarco @zzarcon

chaosocket's People

Contributors

zzarcon avatar

Watchers

James Cloos avatar Alex Laforge avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.