vue-native-websocket-es5 ·
Fork of https://github.com/nathantsoi/vue-native-websocket#readme but without using ES6 Proxy
native websocket implementation for Vuejs 2 and Vuex
Install
yarn add vue-native-websocket-es5# ornpm install vue-native-websocket-es5 --save
Usage
Configuration
Automatic socket connection from an URL string
Vue
Enable Vuex integration, where './store'
is your local apps store:
Vue
Set sub-protocol, this is optional option and default is empty string.
Vue
Optionally enable JSON message passing:
Vue
JSON message passing with a store:
Vue
Enable ws reconnect automatically:
Vue
Manage connection manually:
Vueconst vm =// Connect to the websocket target specified in the configurationvm// Connect to an alternative websocket URL and Options e.g.vm// do stuff with WebSocketsvm
On Vuejs instance usage
var vm =methods:{// $socket is [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instancethis$socket// or with {format: 'json'} enabledthis$socket}
Dynamic socket event listeners
Create a new listener, for example:
this$optionssockets console
Remove existing listener
delete this$optionssocketsonmessage
Vuex Store integration
Vuex integration works differently depending on if you've enabled a format
Without a format enabled
Socket events will commit mutations on the root store corresponding to the following events
SOCKET_ONOPEN
SOCKET_ONCLOSE
SOCKET_ONERROR
SOCKET_ONMESSAGE
Each callback is passed the raw websocket event object
Update state in the open, close and error callbacks. You can also check the socket state directly with the this.$socket
object on the main Vue object.
Handle all the data in the SOCKET_ONMESSAGE
mutation.
Reconect events will commit mutations SOCKET_RECONNECT
and SOCKET_RECONNECT_ERROR
.
Vue;Vuex
With custom mutation names
// mutation-types.jsconst SOCKET_ONOPEN = '✅ Socket connected!'const SOCKET_ONCLOSE = '❌ Socket disconnected!'const SOCKET_ONERROR = '❌ Socket Error!!!'const SOCKET_ONMESSAGE = 'Websocket message received'const SOCKET_RECONNECT = 'Websocket reconnected'const SOCKET_RECONNECT_ERROR = 'Websocket is having issues reconnecting..'// store.jsVue;Vuex// index.jsconst mutations =SOCKET_ONOPENSOCKET_ONCLOSESOCKET_ONERRORSOCKET_ONMESSAGESOCKET_RECONNECTSOCKET_RECONNECT_ERRORVue
format: 'json'
enabled
With All data passed through the websocket is expected to be JSON.
Each message is JSON.parse
d if there is a data (content) response.
If there is no data, the fallback SOCKET_ON*
mutation is called with the original event data, as above.
If there is a .namespace
on the data, the message is sent to this namespaced: true
store (be sure to turn this on in the store module).
If there is a .mutation
value in the response data, the corresponding mutation is called with the name SOCKET_[mutation value]
If there is an .action
value in the response data ie. action: 'customerAdded'
, the corresponding action is called by name:
actions:{console}
Use the .sendObj({some: data})
method on the $socket
object to send stringified json messages.
Custom socket event handling
Provide you own custom code to handle events received via the passToStoreHandler
option. The function you provide will be passed the following arguments:
- event name
- event
- original/default handler code function
function (eventName, event)
. This allows you to optionally do some basic preprocessing before handing the event over to the original handler.
The original passToStore code is used if no passToStoreHandler
is configured.
Here is an example of passing in a custom handler. This has the original passToStore code to give you an example of what you can do:
Vue
Here is an example of do some preprocessing, then pass the event onto the original handler code:
Vue
Examples
TODO: post your example here!
Credits
Derived from https://github.com/MetinSeylan/Vue-Socket.io