Skip to content

Instantly share code, notes, and snippets.

@probil
Last active May 17, 2018 15:46
Show Gist options
  • Save probil/5fd6433b0e58b8b1ebd9ee2f7a429369 to your computer and use it in GitHub Desktop.
Save probil/5fd6433b0e58b8b1ebd9ee2f7a429369 to your computer and use it in GitHub Desktop.
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
server.listen(3000);
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 Vue from 'vue'
import VueSocketio from 'vue-socket.io-extended'
import io from 'socket.io-client'
Vue.use(VueSocketio, io('http://localhost:3000'))
const App = {
render: () => null,
};
new Vue({
el: '#app',
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)
}
},
mounted() {
setTimeout(() => this.clickButton('Hi there'), 5000);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment