Skip to content

Commit 8e082bb

Browse files
committed
Part 4 - Private Channels
1 parent 1a7ef87 commit 8e082bb

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

app/Events/NewComment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(Comment $comment)
3636
*/
3737
public function broadcastOn()
3838
{
39-
return new Channel('post.'.$this->comment->post->id);
39+
return new PrivateChannel('post.'.$this->comment->post->id);
4040
}
4141

4242
public function broadcastWith()

public/js/app.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ window.Pusher = __webpack_require__(36);
10521052

10531053
window.Echo = new __WEBPACK_IMPORTED_MODULE_0_laravel_echo___default.a({
10541054
broadcaster: 'pusher',
1055-
key: '24828cfb3418ebc5ed17',
1055+
key: '51aaa8133b2080a29f6d',
10561056
cluster: 'mt1',
10571057
encrypted: true
10581058
});
@@ -31921,7 +31921,7 @@ var Connector = function () {
3192131921
key: 'csrfToken',
3192231922
value: function csrfToken() {
3192331923
var selector = void 0;
31924-
if (window && window['Laravel'] && window['Laravel'].csrfToken) {
31924+
if (typeof window !== 'undefined' && window['Laravel'] && window['Laravel'].csrfToken) {
3192531925
return window['Laravel'].csrfToken;
3192631926
} else if (this.options.csrfToken) {
3192731927
return this.options.csrfToken;
@@ -32336,9 +32336,21 @@ var SocketIoConnector = function (_Connector) {
3233632336
createClass(SocketIoConnector, [{
3233732337
key: 'connect',
3233832338
value: function connect() {
32339+
var io = this.getSocketIO();
3233932340
this.socket = io(this.options.host, this.options);
3234032341
return this.socket;
3234132342
}
32343+
}, {
32344+
key: 'getSocketIO',
32345+
value: function getSocketIO() {
32346+
if (typeof io !== 'undefined') {
32347+
return io;
32348+
}
32349+
if (this.options.client !== 'undefined') {
32350+
return this.options.client;
32351+
}
32352+
throw new Error('Socket.io client not found. Should be globally available or passed via options.client');
32353+
}
3234232354
}, {
3234332355
key: 'listen',
3234432356
value: function listen(name, event, callback) {
@@ -32501,7 +32513,7 @@ module.exports = Echo;
3250132513
/***/ (function(module, exports, __webpack_require__) {
3250232514

3250332515
/*!
32504-
* Pusher JavaScript Library v4.2.1
32516+
* Pusher JavaScript Library v4.2.2
3250532517
* https://pusher.com/
3250632518
*
3250732519
* Copyright 2017, Pusher
@@ -32591,7 +32603,7 @@ return /******/ (function(modules) { // webpackBootstrap
3259132603
var _this = this;
3259232604
checkAppKey(app_key);
3259332605
options = options || {};
32594-
if (!options.cluster) {
32606+
if (!options.cluster && !(options.wsHost || options.httpHost)) {
3259532607
var suffix = url_store_1["default"].buildLogSuffix("javascriptQuickStart");
3259632608
logger_1["default"].warn("You should always specify a cluster when connecting. " + suffix);
3259732609
}
@@ -32978,11 +32990,12 @@ return /******/ (function(modules) { // webpackBootstrap
3297832990

3297932991
"use strict";
3298032992
var Defaults = {
32981-
VERSION: "4.2.1",
32993+
VERSION: "4.2.2",
3298232994
PROTOCOL: 7,
3298332995
host: 'ws.pusherapp.com',
3298432996
ws_port: 80,
3298532997
wss_port: 443,
32998+
ws_path: '',
3298632999
sockjs_host: 'sockjs.pusher.com',
3298733000
sockjs_http_port: 80,
3298833001
sockjs_https_port: 443,
@@ -33131,17 +33144,17 @@ return /******/ (function(modules) { // webpackBootstrap
3313133144
args[_i - 0] = arguments[_i];
3313233145
}
3313333146
var message = collections_1.stringify.apply(this, arguments);
33134-
if ((window).console) {
33147+
if (pusher_1["default"].log) {
33148+
pusher_1["default"].log(message);
33149+
}
33150+
else if ((window).console) {
3313533151
if ((window).console.warn) {
3313633152
(window).console.warn(message);
3313733153
}
3313833154
else if ((window).console.log) {
3313933155
(window).console.log(message);
3314033156
}
3314133157
}
33142-
if (pusher_1["default"].log) {
33143-
pusher_1["default"].log(message);
33144-
}
3314533158
}
3314633159
};
3314733160
exports.__esModule = true;
@@ -33841,7 +33854,8 @@ return /******/ (function(modules) { // webpackBootstrap
3384133854
}
3384233855
exports.ws = {
3384333856
getInitial: function (key, params) {
33844-
return getGenericURL("ws", params, getGenericPath(key, "flash=false"));
33857+
var path = (params.httpPath || "") + getGenericPath(key, "flash=false");
33858+
return getGenericURL("ws", params, path);
3384533859
}
3384633860
};
3384733861
exports.http = {
@@ -34227,7 +34241,8 @@ return /******/ (function(modules) { // webpackBootstrap
3422734241
return [
3422834242
[":def", "ws_options", {
3422934243
hostUnencrypted: config.wsHost + ":" + config.wsPort,
34230-
hostEncrypted: config.wsHost + ":" + config.wssPort
34244+
hostEncrypted: config.wsHost + ":" + config.wssPort,
34245+
httpPath: config.wsPath
3423134246
}],
3423234247
[":def", "wss_options", [":extend", ":ws_options", {
3423334248
encrypted: true
@@ -36655,6 +36670,7 @@ return /******/ (function(modules) { // webpackBootstrap
3665536670
wsHost: defaults_1["default"].host,
3665636671
wsPort: defaults_1["default"].ws_port,
3665736672
wssPort: defaults_1["default"].wss_port,
36673+
wsPath: defaults_1["default"].ws_path,
3665836674
httpHost: defaults_1["default"].sockjs_host,
3665936675
httpPort: defaults_1["default"].sockjs_http_port,
3666036676
httpsPort: defaults_1["default"].sockjs_https_port,

resources/assets/js/bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ window.Pusher = require('pusher-js');
4949

5050
window.Echo = new Echo({
5151
broadcaster: 'pusher',
52-
key: '24828cfb3418ebc5ed17',
52+
key: '51aaa8133b2080a29f6d',
5353
cluster: 'mt1',
5454
encrypted: true
5555
});

resources/views/posts/show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
})
8383
},
8484
listen() {
85-
Echo.channel('post.'+this.post.id)
85+
Echo.private('post.'+this.post.id)
8686
.listen('NewComment', (comment) => {
8787
this.comments.unshift(comment);
8888
})

routes/channels.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
Broadcast::channel('App.User.{id}', function ($user, $id) {
1515
return (int) $user->id === (int) $id;
1616
});
17+
18+
Broadcast::channel('post.{id}', function ($user, $id) {
19+
return $user->id == \App\Post::find($id)->user_id;
20+
});

0 commit comments

Comments
 (0)