Skip to content

Commit 79f08a0

Browse files
committed
init
1 parent b1d8174 commit 79f08a0

File tree

5 files changed

+189
-2
lines changed

5 files changed

+189
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,57 @@
1-
# laravel-firebase-broadcast-driver
2-
a firebase broadcast driver for laravel
1+
<h1 align="center">
2+
FireBase Broadcast Driver
3+
<br>
4+
<a href="https://packagist.org/packages/ctf0/firebase-broadcast-driver"><img src="https://img.shields.io/packagist/v/ctf0/firebase-broadcast-driver.svg" alt="Latest Stable Version" /></a> <a href="https://packagist.org/packages/ctf0/firebase-broadcast-driver"><img src="https://img.shields.io/packagist/dt/ctf0/firebase-broadcast-driver.svg" alt="Total Downloads" /></a>
5+
</h1>
6+
7+
## Installation
8+
9+
- `composer require ctf0/firebase-broadcast-driver`
10+
11+
- the package internally use [`kreait/firebase-php`](https://firebase-php.readthedocs.io/en/latest/) to send data to firebase.
12+
13+
### Config
14+
```php
15+
// config/broadcasting
16+
17+
return [
18+
'connections'=> [
19+
// ...
20+
21+
'firebase'=> [
22+
'driver'=> 'firebase',
23+
'apiKey'=> env('FB_API_KEY'),
24+
'databaseURL'=> env('FB_DB_URL'), // the real time database url'projectId'=> env('FB_PROJECT_ID'),
25+
'creds_file'=> env('FB_CREDENTIALS_FILE'),
26+
'collection_name'=> env('FB_COLLECTION_NAME'), // ex.notifications
27+
],
28+
],
29+
];
30+
```
31+
32+
### Usage
33+
34+
- add `BROADCAST_DRIVER=firebase` to `.env`
35+
36+
- atm there no support for [laravel-echo](https://laravel.com/docs/5.8/broadcasting#installing-laravel-echo) "any help is appreciated" but no worries, you still get the same payload as other broadcast drivers.
37+
38+
but you can check the [firebase api docs](https://firebase.google.com/docs/database/web/start) or [vuefire](https://github.com/vuejs/vuefire) if you are using `vue`, on how to listen for changes and update your app users accordingly.
39+
40+
41+
### Notification Data Sample
42+
```json
43+
{
44+
"notifications" : {
45+
"-LkgtAVVw0Ztwyjayd9n" : {
46+
"channels" : [ "private-App.User.091b0f7e-805b-4aab-8c99-445039157783" ],
47+
"data" : {
48+
"body" : "some body",
49+
"id" : "d54c44a2-8a42-43a4-bae0-e2b159d1533b",
50+
"title" : "some title",
51+
"type" : "App\\Notifications\\AlertUser"
52+
},
53+
"event" : "Illuminate\\Notifications\\Events\\BroadcastNotificationCreated"
54+
},
55+
}
56+
}
57+
```

composer.json

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "ctf0/firebase-broadcast-driver",
3+
"description": "a firebase broadcast driver for laravel",
4+
"homepage": "https://github.com/ctf0/laravel-firebase-broadcast-driver",
5+
"license": "MIT",
6+
"keywords": [
7+
"ctf0",
8+
"laravel",
9+
"firebase",
10+
"broadcast"
11+
],
12+
"authors": [
13+
{
14+
"name": "Muah",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"require": {
19+
"php": "~7.0",
20+
"illuminate/support": "5.5 - 5.8",
21+
"kreait/firebase-php": "*",
22+
"ctf0/package-changelog": "*"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"ctf0\\Firebase\\": "src"
27+
}
28+
},
29+
"extra": {
30+
"laravel": {
31+
"providers": [
32+
"ctf0\\Firebase\\FireBaseBroadcastProvider"
33+
]
34+
},
35+
"changeLog": "logs"
36+
},
37+
"config": {
38+
"sort-packages": true
39+
},
40+
"scripts": {
41+
"post-package-install": [
42+
"@php artisan vendor:publish --provider=\"ctf0\\Firebase\\FireBaseBroadcastProvider\""
43+
]
44+
}
45+
}

src/FireBaseBroadcastProvider.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace ctf0\Firebase;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Broadcasting\BroadcastManager;
7+
8+
class FireBaseBroadcastProvider extends ServiceProvider
9+
{
10+
/**
11+
* Perform post-registration booting of services.
12+
*/
13+
public function boot()
14+
{
15+
app(BroadcastManager::class)->extend('firebase', function ($app) {
16+
return new FireBaseBroadcaster();
17+
});
18+
}
19+
20+
public function register()
21+
{
22+
}
23+
}

src/FireBaseBroadcaster.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace ctf0\Firebase;
4+
5+
use Illuminate\Support\Arr;
6+
use Kreait\Firebase\Factory;
7+
use Kreait\Firebase\ServiceAccount;
8+
use Kreait\Firebase\Exception\ApiException;
9+
use Illuminate\Broadcasting\BroadcastException;
10+
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
11+
12+
class FireBaseBroadcaster extends Broadcaster
13+
{
14+
protected $db;
15+
protected $config;
16+
17+
/**
18+
* Create a new broadcaster instance.
19+
*/
20+
public function __construct()
21+
{
22+
$this->config = config('broadcasting.connections.firebase');
23+
24+
$sr_account = ServiceAccount::fromJsonFile(base_path(Arr::get($this->config, 'creds_file')));
25+
26+
$this->db = (new Factory())
27+
->withServiceAccount($sr_account)
28+
->withDatabaseUri(Arr::get($this->config, 'databaseURL'))
29+
->create();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function auth($request)
36+
{
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function validAuthenticationResponse($request, $result)
43+
{
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function broadcast(array $channels, $event, array $payload = [])
50+
{
51+
$db = $this->db->getDatabase();
52+
53+
try {
54+
$db->getReference(Arr::get($this->config, 'collection_name'))->push([
55+
'channels' => $this->formatChannels($channels),
56+
'data' => $payload,
57+
'event' => $event,
58+
]);
59+
} catch (ApiException $e) {
60+
throw new BroadcastException($e);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)