Skip to content

Instantly share code, notes, and snippets.

@mikield
Last active September 27, 2017 07:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikield/45c8997e05b5ab0e71a56009ad955f33 to your computer and use it in GitHub Desktop.
Save mikield/45c8997e05b5ab0e71a56009ad955f33 to your computer and use it in GitHub Desktop.
AdonisJS Laravel Echo Broadcaster
'use strict'
/*
* Application-Broadcaster
*
* (c) Vladyslav Gaysyuk <mikield@icloud.com>
*
* 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
'use strict'
/*
* Application-Broadcaster
*
* (c) Vladyslav Gaysyuk <mikield@icloud.com>
*
* 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
@mikield
Copy link
Author

mikield commented Sep 26, 2017

Usage

const Broadcaster = use('Broadcaster')
Broadcaster
      .event('MyAwesomeEvent')
      .into('MyAwesomeChannel')
      .with({"My Awesome Message"}).broadcast()

Of course from the client side do not use .listen method on channel - because this will listen for App.Events.MyAwesomeEvent event and not for MyAwesomeEvent. Use .on instead.

Even if you want to use .listen method - modify the Broadcaster and append to the event name App.Events.. Should be something like this:

event(event){
    this.broadcastEvent = "App.Events." + event
    return this
  }

As you see in Broadcaster there is method called .to(request) and it takes the socket.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 the sokectId that shall be ignored and it works with the main laravel-echo-server repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment