Last active
September 27, 2017 07:50
-
-
Save mikield/45c8997e05b5ab0e71a56009ad955f33 to your computer and use it in GitHub Desktop.
AdonisJS Laravel Echo Broadcaster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
/* | |
* Application-Broadcaster | |
* | |
* (c) Vladyslav Gaysyuk <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
const { ServiceProvider } = require('@adonisjs/fold') | |
class BoardcastServiceProvider extends ServiceProvider { | |
/** | |
* The register method called by ioc container | |
* as a life-cycle method | |
* | |
* @method register | |
* | |
* @return {void} | |
*/ | |
register () { | |
this.app.bind('Broadcaster', (app) => { | |
const Broadcaster = require('./Broadcaster') | |
return new Broadcaster() | |
}) | |
} | |
} | |
module.exports = BoardcastServiceProvider |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
/* | |
* Application-Broadcaster | |
* | |
* (c) Vladyslav Gaysyuk <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
const Redis = use('Redis') | |
class Broadcaster { | |
into(channel){ | |
this.channel = channel | |
return this; | |
} | |
with(data){ | |
this.data = data | |
return this | |
} | |
event(event){ | |
this.broadcastEvent = event | |
return this | |
} | |
toOther(request){ | |
this.socket = request.plainCookie('io') | |
return this | |
} | |
to(request){ | |
this.broadcastTo = request.plainCookie('io') | |
return this | |
} | |
async broadcast(){ | |
let channel = this.channel || '*' | |
Redis.publish(channel,JSON.stringify({ | |
event: this.broadcastEvent || '*', | |
socket: this.socket || null, | |
to: this.broadcastTo || null, | |
data: this.data || {} | |
})) | |
} | |
} | |
module.exports = Broadcaster |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Of course from the client side do not use
.listen
method on channel - because this will listen forApp.Events.MyAwesomeEvent
event and not forMyAwesomeEvent
. Use.on
instead.Even if you want to use
.listen
method - modify theBroadcaster
and append to the event nameApp.Events.
. Should be something like this:As you see in
Broadcaster
there is method called.to(request)
and it takes thesocket.id
from the cookies. Its kinda private message but for guests of your site.To use it - take a look at
https://github.com/mikield/laravel-echo-server/blob/master/src/echo-server.ts#L224
and
https://github.com/mikield/laravel-echo-server/blob/master/src/echo-server.ts#L179
You can use this package too https://github.com/mikield/laravel-echo-server but I dont garantie latest updates from the main project!
But you can use
.toOther(request)
, it will set thesokectId
that shall be ignored and it works with the mainlaravel-echo-server
repo.