Skip to content

Commit f0c3c1c

Browse files
authored
Merge pull request #63 from BinaryStudioAcademy/feature/pusher
Implement pusher service
2 parents b6d4635 + 85d7237 commit f0c3c1c

File tree

17 files changed

+311
-19
lines changed

17 files changed

+311
-19
lines changed

backend/app/Action/Tweet/AddTweetAction.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Entity\Tweet;
88
use App\Repository\TweetRepository;
99
use Illuminate\Support\Facades\Auth;
10+
use App\Events\TweetAddedEvent;
1011

1112
final class AddTweetAction
1213
{
@@ -24,6 +25,8 @@ public function execute(AddTweetRequest $request): AddTweetResponse
2425
$tweet->text = $request->getText();
2526

2627
$tweet = $this->tweetRepository->save($tweet);
28+
29+
broadcast(new TweetAddedEvent($tweet))->toOthers();
2730

2831
return new AddTweetResponse($tweet);
2932
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
use App\Entity\Tweet;
13+
use App\Http\Presenter\Tweet\TweetArrayPresenter;
14+
use Illuminate\Support\Facades\App;
15+
16+
class TweetAddedEvent implements ShouldBroadcast
17+
{
18+
use Dispatchable, InteractsWithSockets, SerializesModels;
19+
20+
public $tweet;
21+
22+
public function __construct(Tweet $tweet)
23+
{
24+
$this->tweet = App::make(TweetArrayPresenter::class)->present($tweet);
25+
}
26+
27+
public function broadcastAs(): string
28+
{
29+
return 'tweet.added';
30+
}
31+
32+
public function broadcastOn(): PrivateChannel
33+
{
34+
return new PrivateChannel('tweets');
35+
}
36+
}

backend/app/Providers/BroadcastServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class BroadcastServiceProvider extends ServiceProvider
1414
*/
1515
public function boot()
1616
{
17-
Broadcast::routes();
17+
Broadcast::routes([
18+
'middleware' => ['api']
19+
]);
1820

1921
require base_path('routes/channels.php');
2022
}

backend/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"laravel/framework": "5.8.*",
1818
"laravel/tinker": "^1.0",
1919
"pda/pheanstalk": "^4.0",
20+
"pusher/pusher-php-server": "^3.4",
2021
"tymon/jwt-auth": "1.0.*"
2122
},
2223
"require-dev": {

backend/composer.lock

Lines changed: 137 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/config/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
*/
172172
App\Providers\AppServiceProvider::class,
173173
App\Providers\AuthServiceProvider::class,
174-
// App\Providers\BroadcastServiceProvider::class,
174+
App\Providers\BroadcastServiceProvider::class,
175175
App\Providers\EventServiceProvider::class,
176176
App\Providers\RouteServiceProvider::class,
177177

backend/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('tweets', function () {
19+
return true;
20+
});

frontend/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
VUE_APP_API_URL=http://localhost:7777/api/v1
2+
VUE_APP_PUSHER_APP_KEY=
3+
VUE_APP_PUSHER_APP_CLUSTER=eu
4+
VUE_APP_PUSHER_APP_AUTH_ENDPOINT=http://localhost:7777/broadcasting/auth

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"buefy": "^0.7.4",
1717
"core-js": "^2.6.5",
1818
"moment": "^2.24.0",
19+
"pusher-js": "^4.4.0",
1920
"vue": "^2.6.6",
2021
"vue-router": "^3.0.1",
2122
"vuex": "^3.0.1"

frontend/src/api/Api.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import axios from 'axios/index';
22
import Storage from '@/services/Storage';
33
import { EventEmitter, TOKEN_EXPIRED_EVENT } from '@/services/EventEmitter';
44
import { UNAUTHENTICATED } from '@/api/ErrorCodes';
5+
import { getSocketId } from '@/services/Pusher';
56

67
class Api {
7-
constructor(apiUrl, authHeaderName = 'Authorization', authHeaderPrefix = 'Bearer') {
8+
constructor(apiUrl, authHeaderName = 'Authorization') {
89
this.axios = axios.create({ baseURL: apiUrl });
910

1011
this.axios
@@ -13,7 +14,11 @@ class Api {
1314
.use(
1415
config => {
1516
if (Storage.hasToken()) {
16-
config.headers[authHeaderName] = `${authHeaderPrefix} ${Storage.getToken()}`;
17+
config.headers[authHeaderName] = `${Storage.getTokenType()} ${Storage.getToken()}`;
18+
}
19+
20+
if (getSocketId()) {
21+
config.headers['X-Socket-ID'] = getSocketId();
1722
}
1823

1924
return config;
@@ -30,9 +35,7 @@ class Api {
3035
const { response } = errorResponse;
3136

3237
if (!response) {
33-
return Promise.reject({
34-
message: 'Unexpected error!'
35-
});
38+
return Promise.reject(new Error('Unexpected error!'));
3639
}
3740

3841
const error = response.data.errors[0];

0 commit comments

Comments
 (0)