• Stars
    star
    4,168
  • Rank 9,959 (Top 0.3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small decorator for the JavaScript WebSocket API that automatically reconnects

ReconnectingWebSocket

A small JavaScript library that decorates the WebSocket API to provide a WebSocket connection that will automatically reconnect if the connection is dropped.

It is API compatible, so when you have:

var ws = new WebSocket('ws://....');

you can replace with:

var ws = new ReconnectingWebSocket('ws://....');

Minified library with gzip compression is less than 600 bytes.

How reconnections occur

With the standard WebSocket API, the events you receive from the WebSocket instance are typically:

onopen
onmessage
onmessage
onmessage
onclose // At this point the WebSocket instance is dead.

With a ReconnectingWebSocket, after an onclose event is called it will automatically attempt to reconnect. In addition, a connection is attempted repeatedly (with a small pause) until it succeeds. So the events you receive may look something more like:

onopen
onmessage
onmessage
onmessage
onclose
// ReconnectingWebSocket attempts to reconnect
onopen
onmessage
onmessage
onmessage
onclose
// ReconnectingWebSocket attempts to reconnect
onopen
onmessage
onmessage
onmessage
onclose

This is all handled automatically for you by the library.

Parameters

var socket = new ReconnectingWebSocket(url, protocols, options);

url

protocols

options

  • Options (see below)

Options

Options can either be passed as the 3rd parameter upon instantiation or set directly on the object after instantiation:

var socket = new ReconnectingWebSocket(url, null, {debug: true, reconnectInterval: 3000});

or

var socket = new ReconnectingWebSocket(url);
socket.debug = true;
socket.timeoutInterval = 5400;

debug

  • Whether this instance should log debug messages or not. Debug messages are printed to console.debug().
  • Accepts true or false
  • Default value: false

automaticOpen

  • Whether or not the websocket should attempt to connect immediately upon instantiation. The socket can be manually opened or closed at any time using ws.open() and ws.close().
  • Accepts true or false
  • Default value: true

reconnectInterval

  • The number of milliseconds to delay before attempting to reconnect.
  • Accepts integer
  • Default: 1000

maxReconnectInterval

  • The maximum number of milliseconds to delay a reconnection attempt.
  • Accepts integer
  • Default: 30000

####reconnectDecay

  • The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist.
  • Accepts integer or float
  • Default: 1.5

timeoutInterval

  • The maximum time in milliseconds to wait for a connection to succeed before closing and retrying.
  • Accepts integer
  • Default: 2000

maxReconnectAttempts

  • The maximum number of reconnection attempts that will be made before giving up. If null, reconnection attempts will be continue to be made forever.
  • Accepts integer or null.
  • Default: null

binaryType

  • The binary type is required by some applications.
  • Accepts strings 'blob' or 'arraybuffer'.
  • Default: 'blob'

Methods

ws.open()

  • Open the Reconnecting Websocket

ws.close(code, reason)

ws.refresh()

  • Refresh the connection if still open (close and then re-open it).

ws.send(data)

  • Transmits data to the server over the WebSocket connection.
  • Accepts @param data a text string, ArrayBuffer or Blob

Like this? Check out websocketd for the simplest way to create WebSocket backends from any programming language.

Follow @joewalnes

Bitdeli Badge

More Repositories

1

websocketd

Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.
Go
17,086
star
2

smoothie

Smoothie Charts: smooooooth JavaScript charts for realtime streaming data
JavaScript
2,230
star
3

filtrex

A simple, safe, JavaScript Filter Expression compiler for end-users
JavaScript
1,042
star
4

web-vmstats

Prettify vmstats in your browser
JavaScript
1,009
star
5

gcode-viewer

A web-based 3D GCode viewer to help understand 3D printer models.
JavaScript
246
star
6

jstinytest

Tiniest simplest unit testing library for JavaScript
JavaScript
204
star
7

greendots

Write with those green dots on your GitHub profile
Python
184
star
8

tinytest

A tiny unit-testing framework for C
C
170
star
9

jquery-simple-context-menu

A really simple, good looking jQuery context menu
JavaScript
102
star
10

port-sharding

Example of Linux SO_REUSEPORT
Python
90
star
11

go-getter

Like 'go get' but with pinned package versions. A tiny shell script.
Shell
86
star
12

toybrick

OpenSCAD program to generate toy bricks compatible with existing systems
OpenSCAD
85
star
13

node-ldapauth

LDAP authentication library for Node.JS
C++
69
star
14

resisto.rs

No frills resistor color code tool
HTML
67
star
15

webconnect

JavaScript APIs to allow web-apps to connect to hardware devices.
JavaScript
52
star
16

pi-crust

Connect things to your Raspberry Pi
Eagle
47
star
17

idea-community

IntelliJ IDEA Community edition - my hacks
Java
33
star
18

crunchicorn

Simplifying the web toolchain
JavaScript
32
star
19

verifysignature

Sample of standalone portable C to verify Ed25519 public-key signature
C
21
star
20

hackerspace-globe

Mashup: View HackerSpace status on a WebGL Globe
JavaScript
14
star
21

raspberry-play

Playing with the Raspberry Pi
JavaScript
14
star
22

quick2wire-fritzomatic

A tool to quickly generate Fritzing components.
JavaScript
9
star
23

flashprance

A lightweight communication protocol to send data from your computer/phone to an embedded device by flashing the screen
C++
9
star
24

arduino-play

Playing with Arduino and AVR hardware
C
7
star
25

webbit-easyremote

Simple bidirectional JavaServer<->JavaScriptBrowser remoting, based on WebSockets and Webbit
JavaScript
6
star
26

webbit

Webbit Server: Moved....
5
star
27

webbit-asyncio

Event based asynchronous Java IO library - built on libeio
C
5
star
28

msp430-shiftbrite

C / C++ library to allow MSP430 microcontroller to talk to ShiftBrite RGB module
C++
5
star
29

esper-console

Web based Esper console
JavaScript
3
star
30

thor-gui

A web based GUI for Thor
JavaScript
2
star
31

3d-printing

Thingies I've designed for 3D printing
2
star
32

webbit-sitemesh

SiteMesh 3 for Webbit
Java
2
star
33

graphviz2

Joe's modifications to AT&T's GraphViz/Dot
C
2
star
34

c-stuff

C
1
star
35

vim

.vim
Vim Script
1
star
36

pi-plate-clip

Makes the Adafruit's Raspberry Pi Plate just a liiiiittle bit stronger.
1
star
37

openscad-tutorial

A tutorial for OpenSCAD.
1
star
38

test

1
star
39

sitemesh-resources

Additional resources to support SiteMesh project.
1
star
40

domain-watcher

Watches a domain name and notifies me when it's status changes
1
star
41

msp430-rgb-color-cycle

A quick cheap circuit for color cycling an RGB LED using an MSP430 microcontroller.
1
star
42

atx-psu-breakout

Fork of the ATX PSU breakout board from Dangerous Prototypes.
1
star