Skip to content

Instantly share code, notes, and snippets.

@harishankards
Last active July 4, 2018 09:53
Show Gist options
  • Select an option

  • Save harishankards/82c8d103381df5c6f79af15bca8392e0 to your computer and use it in GitHub Desktop.

Select an option

Save harishankards/82c8d103381df5c6f79af15bca8392e0 to your computer and use it in GitHub Desktop.
To debug socket.io implementation with node and Vue
const app = express();
var server = require('http').Server(app);
const io = require('socket.io')(server);
io.on('connection', function(socket){
console.log("---------------------------------------------------------")
console.log('An user connected');
console.log("---------------------------------------------------------")
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('emit_method', function(msg){
console.log('message: ' + msg);
});
});
const server = app.listen(app.get('port'), () => {
console.log('%s App is running at http://localhost:%d in %s mode', chalk.green('✓'), app.get('port'), app.get('env'));
console.log(' Press CTRL-C to stop\n');
});
const express = require('express');
const socket = require('socket.io');
const app = express();
const io = socket(server);
io.on('connection', function(socket){
console.log("---------------------------------------------------------")
console.log('An user connected');
console.log("---------------------------------------------------------")
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('emit_method', function(msg){
console.log('message: ' + msg);
});
});
import VueSocketio from 'vue-socket.io-extended'
import io from 'socket.io-client'
Vue.use(VueSocketio, io('http://localhost:3000'))
new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App },
sockets: {
connect () {
console.log('socket connected')
},
customEmit (val) {
console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)', val)
}
},
methods: {
clickButton (val) {
// this.$socket is `socket.io-client` instance
this.$socket.emit('emit_method', val)
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment