DEV Community

Discussion on: Unconventional Vue—Vue as a Backend Framework

Collapse
 
dtinth profile image
Thai Pangsakulyanont

I love Vue’s reactivity system and I really like to use it in Node.js as well! When we talk about Vue it is usually so tied to the frontend and articles like this is hard to come by.

Today I learned that Vue.observable exists and that in Vue 3 reactivity system will be available in its own package, and the observing facilities will be improved to support things like Object.keys() and adding properties without Vue.set.

Thanks for writing this!

There is just one thing I want to add: We can already use Vue 2’s reactivity system in Node. If we don't mount any component, Vue doesn't require a DOM and we can just require('vue'). I find this very useful when I want to do reactive stuff with Socket.IO. For example:

const Vue = require('vue')

const backendApp = new Vue({
  data: {
    topic: 'Initial topic'
  },
  watch: {
    topic(newTopic) {
      io.sockets.emit('topic', newTopic)
    }
  },
  methods: {
    handleConnection(socket) {
      socket.emit('topic', this.topic)
    }
  }
})

io.on('connect', socket => {
  backendApp.handleConnection(socket)
})