Skip to content
/ wsrecon Public

Isomorphic implementation of an automatically reconnecting websocket client.

License

Notifications You must be signed in to change notification settings

eldoy/wsrecon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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!

About

Isomorphic implementation of an automatically reconnecting websocket client.

Topics

Resources

License

Stars

Watchers

Forks