Created
September 26, 2017 17:06
-
-
Save mikield/b3645f35b89958b0797f1fe389e8112b to your computer and use it in GitHub Desktop.
Notify a user to Private channel with Laravel Echo Server + 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' | |
/* | |
* Applicaiton-Nofifier | |
* | |
* (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 NotificationServiceProvider extends ServiceProvider { | |
/** | |
* The register method called by ioc container | |
* as a life-cycle method | |
* | |
* @method register | |
* | |
* @return {void} | |
*/ | |
register () { | |
const Broadcaster = this.app.use('Broadcaster') | |
this.app.bind('Notification', (app) => { | |
return class Notify { | |
constructor(){ | |
this.Broadcaster = Broadcaster | |
this.Notification = this.Broadcaster.event('notification') | |
} | |
to(user){ | |
this.Notification.into('private-user-state.'+user.id) | |
return this | |
} | |
with(data){ | |
this.Notification.with(data) | |
return this | |
} | |
withError(error){ | |
this.Notification.with({error}) | |
return this | |
} | |
send(){ | |
this.Notification.broadcast() | |
} | |
} | |
}) | |
} | |
} | |
module.exports = NotificationServiceProvider |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before use
Before using this code please take a look at https://gist.github.com/mikield/45c8997e05b5ab0e71a56009ad955f33
Cause this code uses
Broadcaster
service.Subscribe user from client side to private channel, and set it into the code.
For example:
Ho to use?
Or