06.10.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Events 354<br />

8 return 'your-queue-name';<br />

9 }<br />

Consuming Event Broadcasts<br />

Pusher<br />

You may conveniently consume events broadcast using the Pusher¹⁸⁵ driver using Pusher’s JavaScript<br />

SDK. For example, let’s consume the App\Events\ServerCreated event from our previous examples:<br />

1 this.pusher = new Pusher('pusher-key');<br />

2<br />

3 this.pusherChannel = this.pusher.subscribe('user.' + USER_ID);<br />

4<br />

5 this.pusherChannel.bind('App\\Events\\ServerCreated', function(message) {<br />

6 console.log(message.user);<br />

7 });<br />

Redis<br />

If you are using the Redis broadcaster, you will need to write your own Redis pub/sub consumer<br />

to receive the messages and broadcast them using the websocket technology of your choice. For<br />

example, you may choose to use the popular Socket.io¹⁸⁶ library which is written in Node.<br />

Using the socket.io and ioredis Node libraries, you can quickly write an event broadcaster to<br />

publish all events that are broadcast by your Laravel application:<br />

1 var app = require('http').createServer(handler);<br />

2 var io = require('socket.io')(app);<br />

3<br />

4 var Redis = require('ioredis');<br />

5 var redis = new Redis();<br />

6<br />

7 app.listen(6001, function() {<br />

8 console.log('Server is running!');<br />

9 });<br />

¹⁸⁵https://pusher.com<br />

¹⁸⁶http://socket.io

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!