Skip to content

Commit

Permalink
2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
metinseylan committed Dec 30, 2016
1 parent c7491fc commit 8e0100b
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 83 deletions.
90 changes: 67 additions & 23 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
# Vue-Socket.io
socket.io implemantation for Vuejs 2.0 and 1.0

## Install
[![NPM version](https://img.shields.io/npm/v/vue-socket.io.svg)](https://www.npmjs.com/package/vue-socket.io)
![VueJS v2 compatible](https://img.shields.io/badge/Vuejs%202-compatible-green.svg)
<a href="https://www.npmjs.com/package/vue-socket.io"><img src="https://img.shields.io/npm/dt/vue-socket.io.svg" alt="Downloads"></a>
<img id="dependency_badge" src="https://www.versioneye.com/javascript/metinseylan:vue-socket.io/2.0.1/badge.svg" alt="Dependency Badge" rel="nofollow">
<a href="https://www.npmjs.com/package/vue-socket.io"><img src="https://img.shields.io/npm/l/vue-socket.io.svg" alt="License"></a>

``` bash
npm install vue-socket.io --save
```
for Vue 1.0
socket.io implemantation for Vuejs 2 and Vuex

``` bash
npm install vue-socket.io@1.0.2 --save
```
## Install

## Usage
``` bash
npm install vue-socket.io --save
```

## Usage
##### Configration
Automaticly socket connect from url string
``` js
import Vue from 'vue';
import VueSocketio from 'vue-socket.io';

Vue.use(VueSocketio, 'http://socketserver.com:1923'); // Automaticly socket connect from url string

/*
import socketio from 'socket.io-client';
Vue.use(VueSocketio, 'http://socketserver.com:1923');
```

var ioInstance = socketio('http://socketserver.com:1923');
Bind custom socket.io-client instance
``` js
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'));
```

Vue.use(VueSocketio, ioInstance); // bind custom socketio instance
*/
Enable Vuex integration
``` js
import store from './yourstore'
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'), store);
```

##### On Vuejs instance usage
``` js
var vm = new Vue({
sockets:{
connect: function(){
Expand All @@ -46,10 +52,48 @@ var vm = new Vue({
})
```

##### Dynamic socket event listenlers
Create new listenler
``` js
this.$options.sockets.event_name = (data) => {
console.log(data)
}
```
Remove exist listenler
``` js
delete this.$options.sockets.event_name;
```

##### Vuex Store integration
Example store, socket mutations always have "SOCKET_" prefix
``` js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
state: {
connect: false,
message: null
},
mutations:{
SOCKET_CONNECT: (state, status ) => {
state.connect = true;
},
SOCKET_USER_MESSAGE: (state, message) => {
state.message = message;
}
},
actions: {
otherAction: ({ commit, dispatch, state }, type) => {
return true;
}
}
})
```

## Example
[Realtime Car Tracker System](http://metinseylan.com/)

[Simple Chat App](http://metinseylan.com/vuesocketio/)

## License
[WTFPL](http://www.wtfpl.net/)
7 changes: 3 additions & 4 deletions dist/build.js
100644 → 100755

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/build.js.map
100644 → 100755

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "vue-socket.io",
"version": "2.0.1",
"description": "socket.io implemantation for vuejs",
"version": "2.1.0",
"description": "socket.io implemantation for vuejs and vuex",
"main": "dist/build.js",
"scripts": {
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
"build": "webpack --progress --hide-modules"
},
"repository": {
"type": "git",
Expand All @@ -14,9 +14,13 @@
"vuejs",
"socket",
"vue",
"socket",
"socket.io",
"comolokko"
"websocket",
"socket.io-client",
"realtime",
"flux",
"vuex",
"redux"
],
"author": "Metin Seylan",
"license": "MIT",
Expand All @@ -31,8 +35,6 @@
"babel-cli": "^6.11.4",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"cross-env": "^2.0.0",
"webpack": "^1.13.2"
"webpack": "^2.2.0-rc.3"
}
}
1 change: 1 addition & 0 deletions src/Emitter.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export default new class {
}
return false;
}

}
32 changes: 22 additions & 10 deletions src/Main.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,50 @@ import Emitter from './Emitter'

export default {

install(Vue, connection){
install(Vue, connection, store){

if(!connection) throw new Error("[Vue-Socket.io] cannot locate connection")

let observer = new Observer(connection)
let observer = new Observer(connection, store)

Vue.prototype.$socket = observer.Socket;

Vue.mixin({
beforeCreate(){
let _this = this;
let sockets = this.$options['sockets']

this.$options.sockets = new Proxy({}, {
set: (target, key, value) => {
Emitter.addListener(key, value, this)
target[key] = value
return true;
},
deleteProperty: (target, key) => {
Emitter.removeListener(key, this.$options.sockets[key], this)
delete target.key;
return true
}
})

if(sockets){
Object.keys(sockets).forEach(function(key) {
Emitter.addListener(key, sockets[key], _this)
Object.keys(sockets).forEach((key) => {
this.$options.sockets[key] = sockets[key];
});
}

},
beforeDestroy(){
let _this = this;
let sockets = this.$options['sockets']

if(sockets){
Object.keys(sockets).forEach(function(key) {
Emitter.removeListener(key, sockets[key], _this)
Object.keys(sockets).forEach((key) => {
delete this.$options.sockets[key]
});
}
}
})

}

}
}


26 changes: 22 additions & 4 deletions src/Observer.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,49 @@ import Socket from 'socket.io-client'

export default class{

constructor(connection) {
constructor(connection, store) {

if(typeof connection == 'string'){
this.Socket = Socket(connection);
}else{
this.Socket = connection
}

if(store) this.store = store;

this.onEvent()

}

onEvent(){
this.Socket.onevent = (packet) => {
Emitter.emit(packet.data[0], packet.data[1])
}
Emitter.emit(packet.data[0], packet.data[1]);

if(this.store) this.commitStore('SOCKET_'+packet.data[0], packet.data[1])

};

let _this = this;

["connect", "error", "disconnect", "reconnect", "reconnect_attempt", "reconnecting", "reconnect_error", "reconnect_failed", "connect_error", "connect_timeout", "connecting", "ping", "pong"]
.forEach((value) => {
_this.Socket.on(value, (data) => {
Emitter.emit(value, data)
Emitter.emit(value, data);
if(_this.store) _this.commitStore('SOCKET_'+value.toUpperCase(), data)
})
})
}


commitStore(type, payload){

if(type.split('_')[0].toUpperCase() === 'SOCKET'){

if(this.store._mutations[type])
this.store.commit(type, payload)

}

}

}
47 changes: 14 additions & 33 deletions webpack.config.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,24 @@ module.exports = {
library: ['VueSocketio'],
libraryTarget: 'umd'
},
resolveLoader: {
root: path.join(__dirname, 'node_modules'),
},
devtool: "source-map",
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
],
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/,
query: {
presets: ['es2015']
}
},
{
test: /\.json$/,
loader: 'json'
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
},
devtool: 'eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
module.exports.devtool = 'source-map'

module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.optimize.OccurenceOrderPlugin()
])
}

}
}

0 comments on commit 8e0100b

Please sign in to comment.