Async WebSocket for Vue
Send data to server using WebSocket and then wait for the response from the server to resume the function. The function returns a Promise with the data from server. It wasn't tested yet!
Main purpose I've created this: JS:
const response = await this$websocket;console;
## Install
[](https://lbesson.mit-license.org/) [](https://vuejs.org/)
npm install vue-async-websocket-nic --save
## Usage
Register Vue Plugin:
```javascript
import WebSocketVue from 'vue-async-websocket-nic';
//Pass url as second argument, third argument is an optional options object
Vue.use( WebSocketVue, 'ws://localhost:8090', {} );
//Pass valid WebSocket object as second parameter
const webSocket = new WebSocket( 'ws://localhost:8090' );
Vue.use( WebSocketVue, webSocket );
Options
You can pass an object with options to the Vue.use().
const defOptions = 'debug': false 'load-on-start': false 'max-timeout': 10000 'reconnect': true 'max-reconnect-count': 4 'reconnect-delay': 5000 'create-autoid-func': true 'create-id': '_id' 'response-id': '_id';Vue;
Parameter | Value |
---|---|
debug | Boolean. Whether to display additional logs in console or not. |
load-on-start | Boolean. Whether to automatically open connection, or manually. |
max-timeout | Integer. How long to wait for response in ms. Can be overwritten by args in send function. |
reconnect | Boolean. Whether to reconnect automatically when connection is closed or not. |
max-reconnect-count | Integer. How many times to reconnect. |
reconnect-delay | Integer. Time in ms to wait between each reconnect. |
create-autoid-func | Boolean. creating auto ID or object ID. |
create-id | String. If you want in your client code to use different name than '_id' you can specify it here. |
response-id | String. If you want in your server code to use different name than '_id' you can specify it here. |
Sending data
sendAsync( data, args = {} ) Sending data using sendAsync. It returns a Promise, so you can use .then() if you want. Server has to send a message back with '_id' property. Set 'response-id' option if you want to change '_id' to something else.
try const response = await this$websocket; console;catch error console;
You can pass arguments object as the second parameter.
const defaultArgs = 'parse-json': true 'timeout': true 'max-timeout': -1 'hooks': 'bad-websocket': null 'timed-out': null 'callback': 'send-data-only': true ;const response = await this$websocket;
Parameter | Value |
---|---|
parse-json | Boolean. Whether to parse data to JSON or not. |
timeout | Boolean. If false, will wait forever for the response. |
max-timeout | Integer. How long to wait for response in ms. If value is lower than 0, 'max-timeout' option will be used. |
hooks['bad-websocket'] | Callable. Function that will be called when socket is null, or readyState != 1. This value is wrapped in hooks object. |
hooks['timed-out'] | Callable. Function that will be called when response didn't come. This value is wrapped in hooks object. |
callback['send-data-only'] | Boolean. Whether to send only event.data as response, or the whole MessageEvent. This value is wrapped in callback object. |
send( data, callback = null, args = {} ) If you don't want to use async function, you can use send() and pass a callback function. You don't need to pass the callback, so it will only send the data to server. The args object is the same as in asyncSend.
this$websocket;//Or with callbackthis$websocket;
Listeners
Quick Example PHP code:
$client->send( json_encode( [ '_type' => 'some-type', 'msg' => 'Send message with type "some-type" to client'] ) );
JS code:
//Call only for specific typethis$websocket;//Always call on messagethis$websocket;//You can also use empty string '', it will work as 'any'
Run a function whenever a message comes from the server. The message has to have '_type' parameter set. If not, only functions with type 'any' will be called. You can change '_type' to something else by setting options['response-type'] when installing the Vue plugin(Vue.use).
WebSocket messages - onopen, onclose, onerror
this$websocket;
removeEventListener When adding a listener, the function returns an id. You can use it, to remove the listener.
const id = this$websocket;
TODO
Testing, Using Vuex, Examples