Code Monkey home page Code Monkey logo

simple-websockets's Introduction

Statements CI Dependencies Downloads Version

Simple Websockets

What is it for?

It's super easy, super thin client package for event systems in WebSockets to work with simple-websockets-server (inspired by socket.io but with no bloat).

API example #1 - browser & node

import { SimpleWebSocket } from 'simple-websockets';

const socket = new SimpleWebSocket("ws://localhost:123");

socket._socket // Instance of native WebSocket in browser, or ws in node

socket.on("event name", (arg1, arg2, arg3) => {
    // Listen for custom event from server
});

socket.send("event name to send to server", 1, 2, 3, "fourth argument");

API example #2 - browser

import { SimpleWebSocket } from 'simple-websockets';

const webSocket = new WebSocket("ws://localhost:123");

const socket = new SimpleWebSocket(webSocket);

API example #3 - node

import { SimpleWebSocket } from 'simple-websockets';
import WebSocket from 'ws';

const webSocket = new WebSocket("ws://localhost:123");

const socket = new SimpleWebSocket(webSocket);

Documentation

To constructor you can pass the same options as for ws (node) or WebSocket (browser) or the sockets themselves.

socket.send sends to server stringified JSON object

{
    eventName: "event name is the first argument",
    values: []
}

where values is the array of arguments after first argument of send method.

socket.on listens for incoming data that fits the scheme and calls listener.

You can send events without values.

Event name must be non-empty string.

By default socket calls connection and disconnect events on its own (without any arguments)

This package is for now considered feature-complete - probably will not add any features, only bugfixes.

simple-websockets's People

Contributors

dependabot[bot] avatar osztenkurden avatar

Stargazers

 avatar

Watchers

 avatar  avatar

simple-websockets's Issues

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.