Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eldoy/wsrecon
Isomorphic implementation of an automatically reconnecting websocket client.
https://github.com/eldoy/wsrecon
browser client isomorphic javascript reconnecting-websocket spa websocket
Last synced: 6 days ago
JSON representation
Isomorphic implementation of an automatically reconnecting websocket client.
- Host: GitHub
- URL: https://github.com/eldoy/wsrecon
- Owner: eldoy
- License: mit
- Created: 2018-08-12T10:44:30.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-22T15:25:13.000Z (over 3 years ago)
- Last Synced: 2024-10-11T18:19:58.209Z (about 1 month ago)
- Topics: browser, client, isomorphic, javascript, reconnecting-websocket, spa, websocket
- Language: JavaScript
- Homepage:
- Size: 414 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Reconnecting Websocket
Implementation of an isomorphic automatically reconnecting websocket client.
Message format is JSON. Works perfectly with Node.js servers, script or Server Side Rendering.
### Install
```
npm i wsrecon
```### Usage
The client wraps the normal WebSocket browser client or uses the ws Node.js client if not in the browser.```js
const client = require('wsrecon')// Awaits the connection ready
const socket = await client('ws://localhost:6000', {
// Reconnect timeout in ms, set to false to not automatically reconnect
timeout: 1
})// Register events like this
socket.on('open', (event) => {
console.log('Connection open')
})socket.on('close', (event) => {
console.log('Connection closed')
})socket.on('error', (event) => {
console.log('Connection error')
})// Data from server arrives here automatically JSON parsed
socket.on('message', (data, event) => {
console.log('Received message', data)
})// Send message, data arrives in message event
socket.send({ hello: 'socket' })
```MIT licensed. Enjoy!