# socketconnect

> WebSocket wrapper with HTTP repeat request fallback, providing a shared API for sending and receiving data.

Latest version **0.0.1** (published 2016-03-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install socketconnect
pnpm add socketconnect
yarn add socketconnect
bun add socketconnect
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2016-03-06 |
| First published | 2016-03-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tom Hardman |
| Maintainers | tomhardman0 |
| Keywords | WebSocket, REST, HTTP, fallback, repeat, poll |

## Links

- npm: https://www.npmjs.com/package/socketconnect
- Repository: https://github.com/tomhardman0/socketconnect
- Homepage: https://github.com/tomhardman0/socketconnect#readme
- Issues: https://github.com/tomhardman0/socketconnect/issues
- npm.io page: https://npm.io/package/socketconnect

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.0.1 (latest) — 2016-03-06

## README

# SocketConnect
Incredibly simple and extendable WebSocket wrapper with an HTTP repeat request fallback providing a shared API for sending and receiving data. SocketConnect checks if WebSockets are available to the client, if so they perform as normal, if not it falls back to repeat HTTP requests. You can then compare responses and check for new data.

# Documentation

#### Import or load the module

`var SocketConnect = require('socketconnect');`

`import SocketConnect from 'socketconnect';`

#### Create a new instance

`var connection = new SocketConnect(websocketURL, httpFallbackURL);`

If your `websocketURL` works as a HTTP fallback by replacing `ws://` with `http://` there's no need for the second argument as SocketConnect will perform the replacement. However if that's not the case, the second argument is necessary for the HTTP fallback.

### WebSocket / HTTP Shared API

#### Sending messages

`connection.send(data);`

#### Handling data

```javascript
connection.handleData = function(e) {
  console.log('Data:', e);
};
```

#### Set up custom handling environment

There are two flags for checking whether your SocketConnect has used websockets or the fallback. Use some logic like below to separate your custom event handling:

```javascript
if (connection.isWebSocket) {
  // Custom WebSocket handling here
} else if (connection.isHTTP) {
  // Custom HTTP handling here
}
```

The below API is split is split into WS/HTTP, make sure you override them within the correct block of the above logic.

### WebSocket

#### Event handling
You'll want to override the native WebSocket event handlers, you can do this on `connection.ws.*event*`, e.g.:

```javascript
connection.ws.onopen = function() {
  console.info('Connected!');
};
```

The same applies for `onerror`, `onclose`, however you'll handle `onmessage` within the shared API `handleData`.

**IMPORTANT**: You must set `connection.canSend = true;` within the `onopen` event, this ensures you cannot send messages before the connection is established.

### HTTP

SocketConnect falls back to HTTP repeat requests using `XMLHttpRequest`.

#### Setting request interval

The request interval is set to 1000(ms) by default, but you can change that using:

`connection.setReqInterval(5000); // 5 Seconds`

#### Event Handling

SocketConnect provides access to all status updates from `XMLHttpRequest`. You can add any event handler to them using the below:

```javascript
// Response code 0
connection.reqNotInit = function() {
  console.log('Request not initialised.');
};
```

| Response Code   | Meaning                            | SocketConnect Function |
| ----------------|------------------------------------| -----------------------|
| 0               | Request not initialised            | reqNotInit             |
| 1               | Server connection established      | connectionEstablished  |
| 2               | Request received                   | reqReceived            |
| 3               | Processing request                 | processingReq          |

Response code 4 is dealt with within the shared API `handleData`.

---
_Source: https://npm.io/package/socketconnect · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
