Skip to content

Commit 1d967a2

Browse files
committed
first commit
0 parents  commit 1d967a2

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Vue-Socket.io
2+
3+
socket.io implementation for Vuejs 2 and Vuex
4+
5+
## Install
6+
7+
``` bash
8+
npm install vue-socket-io --save
9+
```
10+
11+
## Usage
12+
#### Configuration
13+
Automatic socket connection from an URL string
14+
15+
``` js
16+
import VueSocketio from 'vue-socket-io';
17+
Vue.use(VueSocketio, 'http://socketserver.com:1923');
18+
```
19+
20+
Bind custom socket.io-client instance
21+
``` js
22+
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'));
23+
```
24+
25+
Enable Vuex integration
26+
27+
``` js
28+
import store from './yourstore'
29+
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'), store);
30+
```
31+
32+
#### On Vuejs instance usage
33+
34+
``` js
35+
var vm = new Vue({
36+
sockets:{
37+
connect: function(){
38+
console.log('socket connected')
39+
},
40+
customEmit: function(val){
41+
console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')
42+
}
43+
},
44+
methods: {
45+
clickButton: function(val){
46+
// $socket is socket.io-client instance
47+
this.$socket.emit('emit_method', val);
48+
}
49+
}
50+
})
51+
```
52+
53+
#### Dynamic socket event listeners
54+
55+
Create a new listener
56+
57+
``` js
58+
this.$options.sockets.listener("event_name", (data) => {
59+
console.log(data)
60+
});
61+
```
62+
63+
Remove existing listener
64+
65+
``` js
66+
this.$options.sockets.listener("event_name");
67+
```
68+
69+
#### Vuex Store integration
70+
71+
Socket **mutations** always have `SOCKET_` prefix.
72+
73+
Socket **actions** always have `socket_` prefix and the socket event name is `camelCased` (ex. `SOCKET_USER_MESSAGE` => `socket_userMessage`)
74+
75+
You can use either one or another or both in your store. Namespaced modules are supported.
76+
77+
``` js
78+
import Vue from 'vue'
79+
import Vuex from 'vuex'
80+
81+
Vue.use(Vuex);
82+
83+
export default new Vuex.Store({
84+
state: {
85+
connect: false,
86+
message: null
87+
},
88+
mutations:{
89+
SOCKET_CONNECT: (state, status ) => {
90+
state.connect = true;
91+
},
92+
SOCKET_USER_MESSAGE: (state, message) => {
93+
state.message = message;
94+
}
95+
},
96+
actions: {
97+
otherAction: (context, type) => {
98+
return true;
99+
},
100+
socket_userMessage: (context, message) => {
101+
context.dispatch('newMessage', message);
102+
context.commit('NEW_MESSAGE_RECEIVED', message);
103+
if (message.is_important) {
104+
context.dispatch('alertImportantMessage', message);
105+
}
106+
...
107+
}
108+
}
109+
})
110+
```
111+
112+
## Note
113+
114+
Initialy a fork of the [repository: Vue-Socket.io](https://github.com/MetinSeylan/Vue-Socket.io)

0 commit comments

Comments
 (0)