简体   繁体   English

VueJS + Django 频道

[英]VueJS + Django Channels

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.我刚刚阅读完VueJSDjango Channels的介绍,并希望将它们一起使用来为网页上的多个组件提供实时更新。 This illustrates the basic idea:这说明了基本思想:

在此处输入图像描述

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.作为 VueJS 的新手,上图似乎需要某种类型的“中间人”,位于 VueJS 组件和 websocket 之间,以确保每个组件都获得正确的数据。

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?如果是这样,VueJS 是否可以充当那个“中间人”来管理哪个组件连接到哪个通道?

Thanks for your help :)谢谢你的帮助 :)

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.但是你可以(如果通过通道有很多更新)最好使用 Vuex 并用套接字数据提供它。 Then if everything is wired correctly your Vue app would be just the view layer that reacts ( no pun intended ) to changes.然后,如果一切都正确连接,您的 Vue 应用程序将只是对更改做出反应(没有双关语)的视图层。

Django channels are just queues ( first in first out ). Django 频道只是队列(先进先出)。 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?如何在 Vue 中收集这些数据?

What I did was set up Vuex.我所做的是设置 Vuex。 I've then made a Vuex plugin called createWebSockets.js .然后我制作了一个名为createWebSockets.js的 Vuex 插件。 When you go through the docs of Vuex and Vuex plugins you'll see that plugin has access to Vuex commit method.当您浏览 Vuex 和 Vuex 插件的文档时,您会看到该插件可以访问 Vuex commit方法。 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.在所述插件中,我打开了我在服务器上运行的通道的套接字,每当有新消息通过时,我只是将数据推送到 Vuex 中,而我的 Vue 应用程序只是对这些更改做出反应。

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:因此,在您熟悉 Vuex 并将其添加到您的应用程序后,您可以执行以下操作:

// 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.这就是您通过套接字更新 Vuex 的方式。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM