Answer the question
In order to leave comments, you need to log in
How to properly connect the vue-native-websocket library and how to properly organize data exchange?
Hello
, I am learning to work with Vue.js
and gradually I understand that with him the chances of shooting myself in the foot are much higher than with React.js
Now my task is to establish client-server communication through web-socket
.
Connection question:
I was looking for a library that would integrate with Vue
, I myself still do not understand it well enough to integrate something with it.
The most popular one is vue-native-websocket , but they don't have any examples to explain different aspects of the interaction.
According to the documentation that is, I understood how to register the library:
I want interaction with to API
go through Vuex
in the Json
.
import Vue from 'vue';
import VueWebsocket from 'vue-native-websocket';
import store from '../store/globalStore';
const server = 'ws:localhost:9998';
const configuration = {
store: store,
format: 'json',
reconnection: true, // (Boolean) whether to reconnect automatically (false)
reconnectionAttempts: 5, // (Number) number of reconnection attempts before giving up (Infinity),
reconnectionDelay: 3000, // (Number) how long to initially wait before attempting a new (1000)
}
Vue.use(VueWebsocket, server, configuration);
Vuex
, because the documentation describes the connection of a new store instance, in one file with the initialization of the Vue
. globalStore.js
(It is exported to the constructor Vue
).import Vue from 'vue'
import Vuex, {mapState, mapActions, mapGetters, mapMutations} from 'vuex'
import interfaceHeader from './modules/headerStore';
import interfaceLeftSidebar from './modules/leftSidebarStore';
import interfaceRightSidebar from './modules/rightSidebarStore';
import websocket from './modules/websocketStore';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
interfaceHeader,
interfaceLeftSidebar,
interfaceRightSidebar,
websocket,
},
});
state, mutations, actions
. mapGetters/Actions/Mutations/State
. Store._modules.root._children.websocket
, but as far as I understand, there is a proxying function and it doesn't like it . Therefore, I disabled the module and passed the entire storage to the store fieldthis.store[n] is not a function
namespaced
websoket
actions: {
sendMessage: function(context, message) {
Vue.prototype.$socket.sendObj({text: 'someone clicked a button'})
}
}
, because the modules apparently do not know anything from the store
pro Vue
, I have to drag the websocket module intoimport Vue from 'vue';
SOCKET_ONOPEN
, but dispatch
sendMessage
a send occurs. import Vue from 'vue';
import App from './App.vue';
import store from './store/globalStore.js';
import {router} from './routes.js';
import VueWebsocket from 'vue-native-websocket';
const server = 'ws://localhost:9998/';
const configuration = {
store: store,
format: 'json',
reconnection: true, // (Boolean) whether to reconnect automatically (false)
reconnectionAttempts: 5, // (Number) number of reconnection attempts before giving up (Infinity),
reconnectionDelay: 3000, // (Number) how long to initially wait before attempting a new (1000)
}
Vue.use(VueWebsocket, server, configuration);
new Vue({
el: '#app',
store,
router,
render: h => h(App),
});
Vuex
, I now have to call the module every time any of my components needs data, websocket
and communicate through it, sending from the server, in addition to the data, another namespace (and probably something else) . API
and working with this either, or something on building a suitable architecture, or God forbid, he personally fits in and explains what's what .. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question