All Projects → houd1ni → WebsocketPromisify

houd1ni / WebsocketPromisify

Licence: MIT license
Makes websocket's API just like REST with Promise-like API, with native Promises.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to WebsocketPromisify

do
Simplest way to manage asynchronicity
Stars: ✭ 33 (+83.33%)
Mutual labels:  asynchronous, promise, thenable, await
relaks
Asynchrounous React component
Stars: ✭ 49 (+172.22%)
Mutual labels:  asynchronous, promise, await
Promise
Asynchronous Programming with Promises
Stars: ✭ 15 (-16.67%)
Mutual labels:  asynchronous, promise, await
Qtpromise
Promises/A+ implementation for Qt/C++
Stars: ✭ 137 (+661.11%)
Mutual labels:  asynchronous, promise
Restbed
Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
Stars: ✭ 1,551 (+8516.67%)
Mutual labels:  asynchronous, websocket-api
Kitchen Async
A Promise library for ClojureScript, or a poor man's core.async
Stars: ✭ 128 (+611.11%)
Mutual labels:  asynchronous, promise
Csp
Communicating Sequential Processes in JavaScript
Stars: ✭ 33 (+83.33%)
Mutual labels:  asynchronous, await
Coerce Rs
Coerce - an asynchronous (async/await) Actor runtime and cluster framework for Rust
Stars: ✭ 231 (+1183.33%)
Mutual labels:  asynchronous, await
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (+700%)
Mutual labels:  asynchronous, promise
try-to-catch
functional try-catch wrapper for promises
Stars: ✭ 30 (+66.67%)
Mutual labels:  promise, await
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (+272.22%)
Mutual labels:  sockets, websocket-api
Workerman
An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols. PHP>=5.3.
Stars: ✭ 9,617 (+53327.78%)
Mutual labels:  asynchronous, ws
Cofx
A node and javascript library that helps developers describe side-effects as data in a declarative, flexible API.
Stars: ✭ 72 (+300%)
Mutual labels:  asynchronous, promise
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (+638.89%)
Mutual labels:  asynchronous, promise
Before After Hook
wrap methods with before/after hooks
Stars: ✭ 49 (+172.22%)
Mutual labels:  asynchronous, promise
Metasync
Asynchronous Programming Library for JavaScript & Node.js
Stars: ✭ 164 (+811.11%)
Mutual labels:  asynchronous, promise
socketwrapper
Async/Sync networking library including UDP, TCP and TLS/TCP socket classes written in C++ 17.
Stars: ✭ 33 (+83.33%)
Mutual labels:  sockets, asynchronous
Message Io
Event-driven message library for building network applications easy and fast.
Stars: ✭ 321 (+1683.33%)
Mutual labels:  sockets, asynchronous
bluff
🙏 Promise A+ implementation
Stars: ✭ 14 (-22.22%)
Mutual labels:  promise, thenable
Vue Loadable
⏳ Improve your loading state control with pretty simple methods and helpers.
Stars: ✭ 23 (+27.78%)
Mutual labels:  asynchronous, promise

WebsocketPromisify

Build Status codecov bundlephobia npm Deps DevDeps

A nice-looking this readme version: https://houd1ni.github.io/WebsocketPromisify/

Makes websocket's API just like REST with Promise-like API, with native Promises. Has a lot of yummies and very lightweight (less than 3kb in gzip)!

const responseData = await ws.send({catSaid: 'Meow!'})

// If you detected some bug, want some stuff to be added, feel free to open an issue! Large data support (chunking), plugins, streams and different server-side implementations are coming. To see a Node.js server-side part example, please, take a look on test/mock in github repo.

Makes a Promise-like WebSocket connection. Features (almost all are tunable via constructor config below.)

  • Async/await ready.
  • ES-module and commonjs built-in.
  • Types (d.ts) included.
  • Automatically reconnects.
  • Supports existent native WebSocket or ws-like implementation (ws npm package) via socket property.
  • And provide your own socket instance via socket config prop.
  • Any id and data keys to negotiate with your back-end.
  • Any (serialiser)/Decoder(deserialiser).
  • Lazy connect: connects only if something sent, then send all of them!
  • Supports middleware-adapter. E.g. you can use 'ws' package in Node!
  • Custom easy .on method with or without condition: analog to .addEventListener.
  • Can log messages/frames/response time into console or wherever you want to. (Hello, firefox 57+!)
  • Any protocols field.
  • Rejects if sent into closed socket or after some timeout without response.
  • If something sent before connection is estabilished, it sends when it's ready.
  • Pings to stay connected if necessary.

How it on Server Side ?

  1. Serialized JSON is sent by this lib = {id: 'generated_id', data: your data}
     ... or some entity from your .encode function(message_id, message_data)
  2. Some Server processing...
  3. Serialized JSON is sent back by the Server = {id: 'the same generated_id', data: feedback data}
     ... or some entity that could be parsed by your .decode function(raw_data)

Default constructor config is

{
  // You can also use plain text and blobs in future.
  data_type: 'json',
  // Debug features. Not required.
    log: ((event, time, message) => null),
    // Will count milliseconds for responses and put them to log function above.
    timer: false,
  // Set up.
    // Required. URL to connect without a protocol.
    // Can start with /, then current page host and port will be used.
    url: 'localhost',
    // Timeout after sending a message before it drops with error.
    timeout: 1400,
    // Reconnect timeout in seconds or null.
    reconnect: 2,
    // Lazy connect: connects only if something sent (then sends all of them!)
    lazy: false,
    // Existing socket if you already have one to augment with this force.
    socket: null,
    // You can set your own middleware here.
    adapter: ((host, protocols) => new WebSocket(host, protocols)),
    // You can replace original serialisation to your own or even binary stuff.
    encode: (message_id, message_data, config) => data,
    // You can replace original deserialisation to your own or even
    //     making the message object from binary data.
    //     id_key and data_key could be taken from the config argument.
    decode: (raw_message) => { message_id, message_data },
    // WebSocket constructor's protocol field.
    protocols: [],
    // Unique id's and data keys to negotiate with back-end.
    server: {
      id_key: 'id',
      data_key: 'data'
    },
    // Pings to avoid interruptions. null to disable.
    ping: {
      interval: 55, // seconds.
      content: {} // goes to `data` => { id, data: {} } by default.
    }
}

Fields/Props:

  // read-only, returns WebSocket (or so) instance to use with other stuff.
  socket

Methods:

  // Returns Promise that connection is open. Works even if it already opened.
  ready()
  // sends any type of message and returns a Promise.
  send(message),
  // .addEventListener with optional predicate that works after reconnections.
  on(event_name, handler, predicate = (WebSocketEvent) => true),
  // Closes the connection and free up memory. Returns Promise that it has been done.
  close()

Example:

  import WSP from 'wspromisify' // or const WSP = require('wspromisify') in Node.

  const somehost = 'example.com:8080'

  const someFunction = async () => {
    const ws = new WSP({
      // If url starts with /,
      // it results in ws(s if in https)://currentHost:currentPort/thisUrl
      url: 'ws://example.com/ws',
      timeout: 2e3, // 1400ms by default.
      timer: true, // false by default.
      // To log data trips. Events: open, close, send, reconnect, error.
      // If timer isn't enabled, the signature is log(event, message)
      log(event, time, message = '') {
        if(time !== null) {
          console.log(event, `in ${time}ms`, message)
        } else {
          console.log(event, message)
        }
      }
    })

    try {
      // You can wait for ready by calling await ws.ready() or send it right now:
      // the messages will be sent as soon as the connection is opened.
      const data = await ws.send({catSaid: 'Meow!'})
      console.log({data})
    } catch(error) {
      console.error('Cannot send a message due to ', error)
    }
  }

  someFunction()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].