Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit 147df83

Browse files
author
Maxim Kerstens
committed
initial commit
1 parent fe4dcf3 commit 147df83

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules/
2+
npm-debug.log
3+
.idea/
4+
.DS_Store
5+
.idea
6+
yarn.lock

README.md

+91
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,93 @@
11
# vue-echo
22
Vue integration for the Laravel Echo library.
3+
4+
## Install
5+
6+
``` bash
7+
npm install vue-echo --save
8+
```
9+
10+
## Usage
11+
12+
### Initialise
13+
First you'll need to register the plugin and optionally initialise the Echo instance.
14+
15+
``` js
16+
import VueEcho from 'vue-echo';
17+
18+
Vue.use(VueEcho, {
19+
broadcaster: 'pusher',
20+
key: 'PUSHER KEY',
21+
});
22+
23+
/**
24+
* Alternatively you can pass an echo instance:
25+
* ********************************************
26+
* import Echo from 'laravel-echo';
27+
*
28+
* let EchoInstance = new Echo({
29+
* broadcaster: 'pusher',
30+
* key: 'PUSHER KEY',
31+
* });
32+
* Vue.use(VueEcho, EchoInstance);
33+
*/
34+
```
35+
36+
### Using Echo
37+
Once vue-echo is registered, every vue instance is able to to subscribe to channels and listen to events through the `this.$echo` property on the connection you specified earlier.
38+
39+
40+
### Subscribe your Vue instance to a single channel
41+
You can subscribe a vue instance to a single standard channel if needed and define your events.
42+
43+
```js
44+
var vm = new Vue({
45+
channel: 'blog'
46+
echo: {
47+
'BlogPostCreated': payload => {
48+
console.log('blog post created', payload);
49+
},
50+
'BlogPostDeleted': payload => {
51+
console.log('blog post deleted', payload);
52+
}
53+
}
54+
})
55+
```
56+
57+
#### Subscribing to channels
58+
59+
Laravel echo allows you to subscribe to; normal, private and presence channels.
60+
61+
In the example above we subscribed to a standard channel.
62+
63+
##### Private channel
64+
If you would like to subscribe to private channel instead, prefix your channel name with `private`:
65+
66+
```js
67+
var vm = new Vue({
68+
channel: 'private:team.1'
69+
echo: {
70+
'BlogPostCreated': payload => {
71+
console.log('blog post created', payload);
72+
},
73+
'BlogPostDeleted': payload => {
74+
console.log('blog post deleted', payload);
75+
}
76+
}
77+
})
78+
```
79+
80+
##### Presence channel
81+
82+
If you would like to subscribe to presence channel instead, prefix your channel name with `presence`:
83+
84+
```js
85+
var vm = new Vue({
86+
channel: 'presence:team.1.chat'
87+
echo: {
88+
'NewMessage': payload => {
89+
console.log('bNew message from team', payload);
90+
}
91+
}
92+
})
93+
```

package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "vue-echo",
3+
"version": "0.1.0",
4+
"description": "Vue integration for the Laravel Echo library.",
5+
"main": "vue-echo.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/happyDemon/vue-echo"
9+
},
10+
"keywords": [
11+
"laravel",
12+
"pusher",
13+
"socket.io",
14+
"vue"
15+
],
16+
"author": {
17+
"name": "Maxim Kerstens"
18+
},
19+
"license": "MIT",
20+
"homepage": "https://github.com/happyDemon/vue-echo",
21+
"dependencies": {
22+
"laravel-echo": "^1.0.5",
23+
"pusher-js": "^3.2.1"
24+
}
25+
}

vue-echo.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import Echo from 'laravel-echo';
2+
3+
export default {
4+
install(Vue, options) {
5+
if (!options) {
6+
throw new Error("[Vue-Echo] cannot locate options");
7+
}
8+
9+
if (typeof options !== 'object') {
10+
throw new Error("[Vue-Echo] cannot initiate options");
11+
}
12+
13+
if(typeof options == 'function')
14+
{
15+
Vue.prototype.$echo = options;
16+
}
17+
else
18+
{
19+
20+
Vue.prototype.$echo = new Echo(options);
21+
}
22+
23+
Vue.mixin({
24+
created(){
25+
let channel = this.$options['channel'];
26+
27+
if(channel)
28+
{
29+
if(channel.startsWith('private:'))
30+
{
31+
this.channel = this.$echo.private(channel.replace('private:', ''))
32+
}
33+
else if(channel.startsWith('presence:'))
34+
{
35+
this.channel = this.$echo.join(channel.replace('presence:', ''))
36+
}
37+
else
38+
{
39+
this.channel = this.$echo.channel(channel);
40+
}
41+
42+
let events = this.$options['echo'];
43+
44+
if(events)
45+
{
46+
Object.keys(events).forEach(function(key){
47+
this.channel.listen(key, events[key]);
48+
}, this);
49+
}
50+
}
51+
},
52+
beforeDestroy(){
53+
if(this.channel !== false){
54+
this.channel.unsubscribe();
55+
}
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)