# j-WebSocket

> Send and receive JSON messages through a reconnecting WebSocket.

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: en
Content-SHA256: 411ad10cdc020aa516fc89fc6167c8788b819b6b49323d5caab3f4f64517115c
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: components@910ed1a86f6e72b4f031d3ad0dc7844f9fc5cb8e
Review: usage-reviewed

The component sends and receives JSON through a reconnecting WebSocket. Configure `url`, bind a model path to receive messages, and call the `send` method to transmit objects.

__Configuration__:

- `url {String}` URL for websocket, it can be relative or absolute
- `reconnect {Number}` can contain only a number (milliseconds, default: `3000`)
- `encoder {Boolean}` performs encode/decode messages via `URI` (default: `false`)
- __NEW__: `statsinterval {Number}` stats interval in milliseconds (default: `1000`)
- __NEW__: `stats {String}` a link to method `function({ received: Number, sent: Number })` or path to a variable (measured in bytes)

__Methods__:

- `component.send(obj)` - sends JSON serialized object to the server
- `component.close()` - closes connection
- `component.connect()` - opens connection
- `component.idletime(is)` - __NEW__ can cancel/reopen connection

__Events__:

- `ON('message', function(data) {})` - received data
- `ON('online', function(isOnline) {})` - online/offline

## Usage

```html
<ui-component name="websocket" path="updates.latest"
  config="url:/updates/"></ui-component>
<script>
  function sendUpdate(value) {
    SETTER('websocket/send', { type: 'update', value: value });
  }
</script>
```

Source: [Total.js components](https://github.com/totaljs/components/tree/910ed1a86f6e72b4f031d3ad0dc7844f9fc5cb8e/j-WebSocket). Copyright Peter Širka and contributors. [MIT license](/knowledge-licenses.json).
