title: Vuex & Websockets
body: <p>So currently I am working with VueJS 2 and I am very new with it. Now I was getting some help with some other people, but I am still stuck.</p>
  
  <p>Here is what I want to achieve (example - closely linked to what I want):</p>
  
  <ol>
  <li>I have a NodeJS application that listens on WebSockets. The application listens for connections via WebSocket and will take JSON data, with a command and then a data object with any content needed for that command.</li>
  </ol>
  
  <p>The command for example could be login, and the data be username and password. The login function on the NodeJS application will then take this data, do what it needs and then return it back over the socket, whether it was successful or not, and maybe include an ID and some user information for Vuex to pickup and place in it's state, for the front-end of the application to pick up/use.</p>
  
  <p>Currently I am using this boiler plate: <a href="https://github.com/SimulatedGREG/electron-vue" rel="noreferrer">https://github.com/SimulatedGREG/electron-vue</a></p>
  
  <p>Which has served me very well as a learning curve, due to me wanting to use Vue and Vuex to manage my application and then use WebSockets for managing data to and from the data server.</p>
  
  <p>So if you look at the link I sent in app/src/renderer/ (this is where the main code is for vue and vuex).</p>
  
  <p>A friend of mine added the following code for me as an example and I am stuck trying to get it into vuex as actions and mutations. He made it all in one vue component, so I am struggling on how it works with vuex. As I want to be able to access the (example) loginUser action from anywhere in the application (uses routes for multiple pages/views).</p>
  
  <p>So in the <code>MyApp/app/src/renderer/components/LandingPageView.vue</code></p>
  
  <pre><code>&lt;template&gt;
    &lt;div&gt;
      &lt;img src="./LandingPageView/assets/logo.png" alt="electron-vue"&gt;
      &lt;h1&gt;Welcome.&lt;/h1&gt;
      &lt;current-page&gt;&lt;/current-page&gt;
      &lt;websocket-output&gt;&lt;/websocket-output&gt;
      &lt;versions&gt;&lt;/versions&gt;
      &lt;links&gt;&lt;/links&gt;
    &lt;/div&gt;
  &lt;/template&gt;
  
  &lt;script&gt;
    import CurrentPage from './LandingPageView/CurrentPage'
    import Links from './LandingPageView/Links'
    import Versions from './LandingPageView/Versions'
    import WebsocketOutput from './LandingPageView/WebsocketOutput'
    export default {
      components: {
        CurrentPage,
        Links,
        Versions,
        WebsocketOutput
      },
      name: 'landing-page'
    }
  &lt;/script&gt;
  
  &lt;style scoped&gt;
    img {
      margin-top: -25px;
      width: 450px;
    }
  &lt;/style&gt;
  </code></pre>
  
  <p>That is the updated file for that, and then below is the code for the <code>MyApp/app/src/renderer/components/LandingPageView/WebsocketOutput.vue</code></p>
  
  <pre><code>&lt;template&gt;
    &lt;div&gt;
      &lt;h2&gt;Socket output:&lt;/h2&gt;
      &lt;p v-text="socket_out"&gt;&lt;/p&gt;
    &lt;/div&gt;
  &lt;/template&gt;
  
  &lt;script&gt;
    var ws = require("nodejs-websocket")
  
    export default {
      data () {
        return {
          socket_out: "connecting to the websocket server..."
        }
      },
      mounted () {
        const parent = this
        var connection = ws.connect("ws://dannysmc.com:9999", {}, function (conn) {})
        connection.on("text", function (text) {
          console.log('Text received: ' + text)
          parent.socket_out = text
        })
        connection.on("connect", function () {
          connection.send('yo')
        })
      },
      created () {
        // Set $route values that are not preset during unit testing
        if (process.env.NODE_ENV === 'testing') {
          this.$route = {
            name: 'websocket-output',
            path: '/websocket-output'
          }
        }
      }
    }
  &lt;/script&gt;
  
  &lt;style scoped&gt;
    code {
      background-color: rgba(46, 56, 76, .5);
      border-radius: 3px;
      color: #fff;
      font-weight: bold;
      padding: 3px 6px;
      margin: 0 3px;
      vertical-align: bottom;
    }
  
    p {
      line-height: 24px;
      color: red;
    }
  &lt;/style&gt;
  </code></pre>
  
  <p>Everything else is just the boiler plate that you see, so if anyone is willing to help me and give me some tips of what to read that explains this or anything else? as I can't find much information on it unfortunately.</p>