{"id":23719358,"url":"https://github.com/houd1ni/websocketpromisify","last_synced_at":"2025-03-17T16:11:24.464Z","repository":{"id":48636486,"uuid":"107286045","full_name":"houd1ni/WebsocketPromisify","owner":"houd1ni","description":"Makes websocket's API just like REST with Promise-like API, with native Promises.","archived":false,"fork":false,"pushed_at":"2025-02-05T08:47:45.000Z","size":770,"stargazers_count":26,"open_issues_count":14,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-02T06:23:17.311Z","etag":null,"topics":["async","asynchronous","await","es2015","javascript","js","native-promises","promise","sockets","strongly-typed","thenable","typed-js","typescript","websocket","websocket-api","websockets","ws","wspromisify"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/houd1ni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-17T15:13:40.000Z","updated_at":"2025-02-05T08:47:48.000Z","dependencies_parsed_at":"2025-02-05T09:23:17.880Z","dependency_job_id":"4186827f-5c28-4db0-814b-6e170fc5ba36","html_url":"https://github.com/houd1ni/WebsocketPromisify","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houd1ni%2FWebsocketPromisify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houd1ni%2FWebsocketPromisify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houd1ni%2FWebsocketPromisify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houd1ni%2FWebsocketPromisify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/houd1ni","download_url":"https://codeload.github.com/houd1ni/WebsocketPromisify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244066180,"owners_count":20392406,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","asynchronous","await","es2015","javascript","js","native-promises","promise","sockets","strongly-typed","thenable","typed-js","typescript","websocket","websocket-api","websockets","ws","wspromisify"],"created_at":"2024-12-30T21:50:02.945Z","updated_at":"2025-03-17T16:11:24.454Z","avatar_url":"https://github.com/houd1ni.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WebsocketPromisify\n[![Scrutinizer build](https://scrutinizer-ci.com/g/houd1ni/WebsocketPromisify/badges/build.png?b=master)](https://scrutinizer-ci.com/g/houd1ni/WebsocketPromisify/build-status/master) [![Scrutinizer code quality](https://scrutinizer-ci.com/g/houd1ni/WebsocketPromisify/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/houd1ni/WebsocketPromisify/code-structure/master) [![codecov](https://codecov.io/gh/houd1ni/WebsocketPromisify/branch/master/graph/badge.svg)](https://codecov.io/gh/houd1ni/WebsocketPromisify) [![bundlejs](https://deno.bundlejs.com/badge?q=wspromisify@2.4.4\u0026treeshake=[*])](https://deno.bundlejs.com/badge?q=wspromisify@2.4.4\u0026treeshake=[*])  [![npm](https://badgen.net/npm/v/wspromisify)](https://www.npmjs.com/package/wspromisify)\n\nA nice-looking this readme version: https://houd1ni.github.io/WebsocketPromisify/\n\nMakes websocket's API just like REST with Promise-like API, with native Promises.\nHas a lot of yummies and very lightweight (less than 3kb in gzip)!\n\n```javascript\nconst responseData = await ws.send({catSaid: 'Meow!'})\n```\n\n// If you detected some bug, want some stuff to be added, feel free to open an issue!\nLarge data support (chunking), plugins, streams and different server-side implementations are coming.\nTo see a Node.js server-side part example, please, take a look on test/mock in github repo.\n\n\nMakes a Promise-like WebSocket connection.\nFeatures (almost all are tunable via constructor config below.)\n- Async/await ready.\n- ES-module and commonjs built-in.\n- Types (d.ts) included.\n- Automatically reconnects.\n- Supports existent native WebSocket or ws-like implementation (ws npm package) via `socket` property.\n- And provide your own socket instance via socket config prop.\n- Any id and data keys to negotiate with your back-end.\n- Any (serialiser)/Decoder(deserialiser).\n- Lazy connect: connects only if something sent, then send all of them!\n- Supports middleware-adapter. E.g. you can use 'ws' package in Node!\n- Custom easy .on method with or without condition: analog to .addEventListener.\n- Can log messages/frames/response time into console or wherever you want to. (Hello, firefox 57+!)\n- Any protocols field.\n- Rejects if sent into closed socket or after some timeout without response.\n- If something sent before connection is estabilished, it sends when it's ready.\n- Pings to stay connected if necessary.\n\nHow it on Server Side ?\n```\n  1. Serialized JSON is sent by this lib = {id: 'generated_id', data: your data}\n     ... or some entity from your .encode function(message_id, message_data)\n  2. Some Server processing...\n  3. Serialized JSON is sent back by the Server = {id: 'the same generated_id', data: feedback data}\n     ... or some entity that could be parsed by your .decode function(raw_data)\n```\n\n\nDefault constructor config is\n```javascript\n{\n  // You can also use plain text and blobs in future.\n  data_type: 'json',\n  // Debug features. Not required.\n    log: ((event, time, message) =\u003e null),\n    // Will count milliseconds for responses and put them to log function above.\n    timer: false,\n  // Set up.\n    // Required. URL to connect without a protocol.\n    // Can start with /, then current page host and port will be used.\n    url: 'localhost',\n    // Timeout after sending a message before it drops with error.\n    timeout: 1400,\n    // Reconnect timeout in seconds or null.\n    reconnect: 2,\n    // Lazy connect: connects only if something sent (then sends all of them!)\n    lazy: false,\n    // Existing socket if you already have one to augment with this force.\n    socket: null,\n    // You can set your own middleware here.\n    adapter: ((host, protocols) =\u003e new WebSocket(host, protocols)),\n    // You can replace original serialisation to your own or even binary stuff.\n    encode: (message_id, message_data, config) =\u003e data,\n    // You can replace original deserialisation to your own or even\n    //     making the message object from binary data.\n    //     id_key and data_key could be taken from the config argument.\n    decode: (raw_message) =\u003e { message_id, message_data },\n    // WebSocket constructor's protocol field.\n    protocols: [],\n    // Unique id's and data keys to negotiate with back-end.\n    server: {\n      id_key: 'id',\n      data_key: 'data'\n    },\n    // Pings to avoid interruptions. null to disable.\n    ping: {\n      interval: 55, // seconds.\n      content: {} // goes to `data` =\u003e { id, data: {} } by default.\n    }\n}\n```\n\nFields/Props:\n```javascript\n\n  // read-only, returns WebSocket (or so) instance to use with other stuff.\n  socket\n```\n\nMethods:\n```javascript\n\n  // Returns Promise that connection is open. Works even if it already opened.\n  ready()\n  // sends any type of message and returns a Promise.\n  send(message),\n  // .addEventListener with optional predicate that works after reconnections.\n  on(event_name, handler, predicate = (WebSocketEvent) =\u003e true),\n  // Closes the connection and free up memory. Returns Promise that it has been done.\n  close()\n\n```\n\nExample:\n```javascript\n\n  import WSP from 'wspromisify' // or const WSP = require('wspromisify') in Node.\n\n  const somehost = 'example.com:8080'\n\n  const someFunction = async () =\u003e {\n    const ws = new WSP({\n      // If url starts with /,\n      // it results in ws(s if in https)://currentHost:currentPort/thisUrl\n      url: 'ws://example.com/ws',\n      timeout: 2e3, // 1400ms by default.\n      timer: true, // false by default.\n      // To log data trips. Events: open, close, send, reconnect, error.\n      // If timer isn't enabled, the signature is log(event, message)\n      log(event, time, message = '') {\n        if(time !== null) {\n          console.log(event, `in ${time}ms`, message)\n        } else {\n          console.log(event, message)\n        }\n      }\n    })\n\n    try {\n      // You can wait for ready by calling await ws.ready() or send it right now:\n      // the messages will be sent as soon as the connection is opened.\n      const data = await ws.send({catSaid: 'Meow!'})\n      console.log({data})\n    } catch(error) {\n      console.error('Cannot send a message due to ', error)\n    }\n  }\n\n  someFunction()\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoud1ni%2Fwebsocketpromisify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoud1ni%2Fwebsocketpromisify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoud1ni%2Fwebsocketpromisify/lists"}