svelte-websocket-store
Version:
svelte store with a websocket backend
80 lines (53 loc) • 3.25 kB
Markdown
[](https://svelte.dev)
[](https://www.npmjs.com/package/svelte-websocket-store)
[](https://opensource.org/licenses/BSD-3-Clause)
[](https://bundlejs.com/?q=svelte-websocket-store)
[](https://npmjs.org/package/svelte-websocket-store)
[](https://github.com/arlac77/svelte-websocket-store/issues)
[](https://actions-badge.atrox.dev/arlac77/svelte-websocket-store/goto)
[](https://github.com/prettier/prettier)
[](http://commitizen.github.io/cz-cli/)
[](https://snyk.io/test/github/arlac77/svelte-websocket-store)
[](https://coveralls.io/github/arlac77/svelte-websocket-store)
[](https://github.com/DevExpress/testcafe)
# svelte-websocket-store
Svelte store with a websocket backend
```js
import websocketStore from "svelte-websocket-store";
const initialValue = { };
export const myStore = websocketStore("wss://mydomain.com/ws1", initialValue, ['option 1', 'option 2']);
// send JSON to websocket server
$myStore = { content: "to be saved", other_values: "all" };
// receive JSON from server (push)
let response = $myStore;
```
# API
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
### Table of Contents
* [websocketStore](#websocketstore)
* [Parameters](#parameters)
## websocketStore
Create a writable store based on a web-socket.
Data is transferred as JSON.
Keeps socket open (reopens if closed) as long as there are subscriptions.
### Parameters
* `url` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the WebSocket url
* `initialValue` **any** store value used before 1st. response from server is present
* `socketOptions` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** transparently passed to the WebSocket constructor
Returns **Store** 
# install
With [npm](http://npmjs.org) do:
```shell
npm install svelte-websocket-store
```
With [yarn](https://yarnpkg.com) do:
```shell
yarn add svelte-websocket-store
```
## run tests
```sh
export BROWSER=safari|chrome|...
npm|yarn test
```
# license
BSD-2-Clause