Snapshot of vitalets/websocket-as-promised: 600★, JavaScript, Frontend. A Promise-based API for WebSockets
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
A WebSocket client library with Promise-based API for browser and Node.js.
Example
import WebSocketAsPromised from 'websocket-as-promised';
// create instance of WebSocket connection
const wsp = new WebSocketAsPromised('ws://example.com');
// wait for WebSocket connection to open
await wsp.open();
// send some data
wsp.send('data');
// wait for connection to close
await wsp.close();
Contents
Requirements
Installation
Usage
Browser
Node.js
Sending data
raw
JSON
binary
request / response
API
Changelog
License
Requirements
global Promise constructor
Installation
npm i websocket-as-promised --save
Usage in browser
import WebSocketAsPromised from 'websocket-as-promised';
const wsp = new WebSocketAsPromised('ws://example.com');
wsp.open()
.then(() => wsp.send('message'))
.then(() => wsp.close())
.catch(e => console.error(e));
const wsp = new WebSocketAsPromised(wsUrl, {
packMessage: data => (new Uint8Array(data)).buffer,
unpackMessage: data => new Uint8Array(data),
});
wsp.open()
.then(() => wsp.sendPacked([1, 2, 3]))
.then(() => wsp.close())
.catch(e => console.error(e));
Sending requests
websocket-as-promised provides simple request-response mechanism (JSON RPC).
Method .sendRequest() sends message with unique requestId and returns promise.
That promise get resolved when response message with the same requestId comes.
For reading/setting requestId from/to message there are two functions defined in options attachRequestId / extractRequestId:
const wsp = new WebSocketAsPromised(wsUrl, {
packMessage: data => JSON.stringify(data),
unpackMessage: data => JSON.parse(data),
attachRequestId: (data, requestId) => Object.assign({id: requestId}, data), // attach requestId to message as `id` field
extractRequestId: data => data && data.id, // read requestId from message `id` field
});
wsp.open()
.then(() => wsp.sendRequest({foo: 'bar'})) // actually sends {foo: 'bar', id: 'xxx'}, because `attachRequestId` defined above
.then(response => console.log(response)); // waits server message with corresponding requestId: {id: 'xxx', ...}
By default requestId value is auto-generated, but you can set it manually:
wsp.sendRequest({foo: 'bar'}, {requestId: 42});
Note: you should implement yourself attaching requestId on server side.
Event channel triggered every time when received message is successfully unpacked.
For example, if you are using JSON transport, the listener will receive already JSON parsed data.
Kind: instance property of WebSocketAsPromised See: https://vitalets.github.io/chnl/#channel Example
Does vitalets/websocket-as-promised have a project website?
No homepage URL was recorded for vitalets/websocket-as-promised in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How active is development on vitalets/websocket-as-promised?
The most recent commit recorded on vitalets/websocket-as-promised was 1.3 years ago, based on the GitHub push timestamp. The repository has 41 forks — one of the better signals of community interest.
How many stars does vitalets/websocket-as-promised have?
vitalets/websocket-as-promised has 600 GitHub stars — refresh the page for the live number, or check github.com/vitalets/websocket-as-promised. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is vitalets/websocket-as-promised open source?
Yes — vitalets/websocket-as-promised ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/vitalets/websocket-as-promised.
What else is in the Frontend space?
vitalets/websocket-as-promised is tracked by TopGit under the Frontend category, alongside 6 GitHub-tagged topics. Trending and Topics pages list peer repositories of comparable stars and language.
What topics is vitalets/websocket-as-promised associated with?
GitHub's repository topics for vitalets/websocket-as-promised: "javascript", "nodejs", "promise", "websocket", "websocket-api", "websocket-client". TopGit's editorial category is Frontend.
Where do I read more about vitalets/websocket-as-promised?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/vitalets/websocket-as-promised is the definitive source.
Read full README in the tab above.
Want a second opinion on websocket-as-promised?
Ask an AI that can read this page — one click and you get its take on websocket-as-promised.