21

I just finished reading the introductions for VueJS and Django Channels and would like to use them together to provide real-time updates to several components on a webpage. This illustrates the basic idea:

enter image description here

Being new to VueJS, it seems the diagram above requires some type of "middle man", between the VueJS components and the websocket, that makes sure each component gets the correct data.

So, my questions are:

  1. Architecturally, is this a good design?
  2. If so, can VueJS act as that "middle man" to manage which component connects to which channel?

Thanks for your help :)

tony19
  • 125,647
  • 18
  • 229
  • 307
fire_water
  • 1,380
  • 1
  • 19
  • 33
  • 3
    Did you create that image yourself? Don't get me wrong, it looks great, but for a stack overflow question, some squiggly lines from microsoft paint is fine. :) . To answer your question, that "middle man" you're looking for is Vuex. Any actions to and from the websocket should flow thru Vuex. – Eric Guan May 25 '17 at 18:06
  • 3
    @EricGuan Yeah, I created the above over-engineered diagram :) I'm a visual learner so diagrams really help me understand concepts. Gonna check out Vuex now. Thanks for your help! – fire_water May 25 '17 at 19:04

1 Answers1

25

No, you don't need a middle man. But you could ( if there is a lot of updates through channel ) be better of with using Vuex and feed it with socket data. Then if everything is wired correctly your Vue app would be just the view layer that reacts ( no pun intended ) to changes.

Django channels are just queues ( first in first out ). You pass whatever data you need to sent to the front end to the channel. All the data gets serialized and passed to the queue. Channel is in worker mode and as soon as it receives a message ( with data ) it tries to emit it on the channel it self.

How to harvest this data in Vue?

What I did was set up Vuex. I've then made a Vuex plugin called createWebSockets.js. When you go through the docs of Vuex and Vuex plugins you'll see that plugin has access to Vuex commit method. In said plugin I opened sockets to channels I've had running on the server and whenever new message came through I just pushed the data in Vuex and my Vue app just reacted to these changes.

I can probably find it somewhere if you need more help.

Best

Edit

So after you get yourself familiar with Vuex and add it to your app you could do something like this:

// plugin code

// importing from node_modules -> you have to install it 
// through npm or yarn
import io from 'socket.io-client'

// opening a socket to an IP. Mind that I've put an 
// example IP here yours will be an IP belonging to the 
// server or 127.0.0.1 if you're working locally
const socket = io('127.0.0.1:4000')

// this is a vuex plugin that takes the store (vuex store) 
// object as its parametar
export default function createWebSockets(socket) {

    // it returns a function to which we passed store object
    return (store) => {

        // this is your channel name on which you want to 
        // listen to emits from back-end
        const channel_name = 'whatever-you-called-it'

        // this opens a listener to channel you named on line above
        socket.on('channel_name', (data) => { // 

            // and this is the store part where you
            // just update your data with data received from socket
            store.commit('YOUR_VUEX_MUTATION', data)

        })

        // you can add multiple socket.on statements if you have more than one channel
    }
}

This is how you would you update Vuex through sockets.

Hope it helps.

peaceman
  • 1,681
  • 12
  • 19
  • 1
    Isn't there any "default" implementation after all that time? We have standardways for everything, but Meteor seems the only simple way to have simple pub/sub / notifications patterns done easily. Is there no vue-socket library? [vue-socket.io](https://github.com/MetinSeylan/Vue-Socket.io) is the only one I found... – nerdoc Oct 24 '18 at 19:25
  • @nerdoc not to my knowledge. Things like this are very specific. You switch a backend framework and a lot of things can change. There are better ways of implementing this for sure but I don't think there is a structured way to do this outside of Meteor (which was built specifically for this front to back ). – peaceman Oct 26 '18 at 00:01
  • 2
    Hm. Really, there are open source implementations of AI and machine learning. But no simple way to use push notifications from a backend other than Meteor... Grrrr. – nerdoc Nov 03 '18 at 15:55