Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/torxed/slimwebsocket
A wrapper around WebSocket for JavaScript
https://github.com/torxed/slimwebsocket
Last synced: about 11 hours ago
JSON representation
A wrapper around WebSocket for JavaScript
- Host: GitHub
- URL: https://github.com/torxed/slimwebsocket
- Owner: Torxed
- License: gpl-3.0
- Created: 2019-10-09T13:33:51.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-07T07:21:38.000Z (over 3 years ago)
- Last Synced: 2024-05-02T06:18:06.208Z (5 months ago)
- Language: JavaScript
- Size: 48.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# slimWebSocket
A wrapper around WebSocket for JavaScript# Example usecases
```html
let socket = new slimWebSocket('wss://obtain.life');
socket.subscribe('certain_key', (json) => {
console.log('Got data containing certain_key:', json);
})
socket.send({"test" : "ping"});```
# Features
* Auto-reconnect
* Simple message queue (with limited re-send error handling)
* Type parsing attempt if you're sending a non-string
* Hook in `socket.socket.addEventListener('message', function(data) {} )` if you need to replace event hooks.# Installation
```html
let socket = null; // Create a global socket element, initate it with `new slimWebSocket();` later
// Loading JavaScript from a cross-site resource is blocked.
// But there's nothing stopping us from downloading the script
// as a text-blob and placing it within the <script> </ script> tags,
// which causes the browser to parse it, but not as a forrain object.
//
// #LoadingScriptsFromGithublet xhr = new XMLHttpRequest();
xhr.open("GET", 'https://raw.githubusercontent.com/Torxed/slimWebSocket/master/slimWebSocket.js', true);
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
let script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = this.responseText;
document.head.appendChild(script);socket = new slimWebSocket();
}
}
xhr.send();```