'where to create a websocket component in vue application

I need to create a websocket component that my UI will use. I only want to establish a connection when the user is logged in, and I want to issue a terminate command when the user logs out of the application. But I am unsure how I can create such a utility so that I can call it from various components. To do this, I am checking the Vuex store, but I'm not clear where I should put my utility.

My project:

- main.js
- App.vue
- components/
  helloWorld.vue

How could I place my code

const WSS = new WebSocket(WEBSOCKET_BASE_URL);

so I can call something like WSS.terminate(), or WSS.send()



Solution 1:[1]

You can create it in App.vue or helloWorld.vue.

You can place your code in created hook. See the example below.

created: function() {
   const wss = new WebSocket("url");
}

You can read the tutorial.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Circuit Breaker