Socket.io client and server module for Nuxt
nuxt-socket-io
dependency to your projectyarn add nuxt-socket-io # or npm install nuxt-socket-io
nuxt-socket-io
to the modules
section of nuxt.config.js
{
modules: [
'nuxt-socket-io',
],
io: {
// module options
sockets: [{
name: 'main',
url: 'http://localhost:3000'
}]
}
}
{
mounted() {
this.socket = this.$nuxtSocket({
channel: '/index'
})
/* Listen for events: */
this.socket
.on('someEvent', (msg, cb) => {
/* Handle event */
})
},
methods: {
method1() {
/* Emit events */
this.socket.emit('method1', {
hello: 'world'
}, (resp) => {
/* Handle response, if any */
})
}
}
}
But WAIT! There’s so much more you can do!! Check out the documentation:
https://nuxt-socket-io.netlify.app/
There you will see:
yarn install
or npm install
yarn dev
or npm run dev
Author: richardeschloss
Demo: https://nuxt-socket-io.netlify.app/
Source Code: https://github.com/richardeschloss/nuxt-socket-io
#vue #vuejs #javascript #nuxtjs #nuxt