Permalink
Browse files

Implemented private channel with socket.io

  • Loading branch information...
1 parent 37cc574 commit 2386f6285347d02644f18c76189a08e3390e596a @takayukii committed Dec 2, 2016
View
@@ -11,7 +11,7 @@ DB_DATABASE=websocket
DB_USERNAME=websocket
DB_PASSWORD=websocket
-BROADCAST_DRIVER=log
+BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Events;
+
+use App\Message;
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+
+class MessageCreatePrivateEvent implements ShouldBroadcast
+{
+ use InteractsWithSockets, SerializesModels;
+
+ public $message;
+
+ /**
+ * Create a new event instance.
+ *
+ * @return void
+ */
+ public function __construct(Message $message)
+ {
+ $this->message = $message;
+ }
+
+ /**
+ * Get the channels the event should broadcast on.
+ *
+ * @return Channel|array
+ */
+ public function broadcastOn()
+ {
+ return [
+ new PrivateChannel('user.' . $this->message->sender->id),
+ new PrivateChannel('user.' . $this->message->receiver->id),
+ ];
+ }
+}
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
+use App\Events\MessageCreatePrivateEvent;
use App\Message;
use App\User;
use Illuminate\Http\Request;
@@ -22,6 +23,8 @@ public function createMessage(User $receiver, Request $request)
$message->receiver()->associate($receiver);
$message->save();
+ event(new MessageCreatePrivateEvent($message));
+
return $message->toJson();
}
@@ -7,6 +7,11 @@
class ApiUsersController extends Controller
{
+ public function me()
+ {
+ return \Auth::user();
+ }
+
public function listUsers()
{
return User::select(['id', 'name'])->get();
@@ -19,7 +19,7 @@ public function boot()
/*
* Authenticate the user's personal channel...
*/
- Broadcast::channel('App.User.*', function ($user, $userId) {
+ Broadcast::channel('user.*', function ($user, $userId) {
return (int) $user->id === (int) $userId;
});
}
View
@@ -6,7 +6,8 @@
"type": "project",
"require": {
"php": ">=5.6.4",
- "laravel/framework": "5.3.*"
+ "laravel/framework": "5.3.*",
+ "predis/predis": "^1.1"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
View
@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "ed9df8b872992035f068a7fe461a635a",
- "content-hash": "76036da3b5b55b91dbe1e1affa01bf9e",
+ "hash": "ed505f794cc52c9b7a35ed0b2faccac0",
+ "content-hash": "7e0e05e84f3eb333685719e2006b48a7",
"packages": [
{
"name": "classpreloader/classpreloader",
@@ -786,6 +786,56 @@
"time": "2016-11-07 23:38:38"
},
{
+ "name": "predis/predis",
+ "version": "v1.1.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/nrk/predis.git",
+ "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/nrk/predis/zipball/f0210e38881631afeafb56ab43405a92cafd9fd1",
+ "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.9"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "~4.8"
+ },
+ "suggest": {
+ "ext-curl": "Allows access to Webdis when paired with phpiredis",
+ "ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "Predis\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Daniele Alessandri",
+ "email": "suppakilla@gmail.com",
+ "homepage": "http://clorophilla.net"
+ }
+ ],
+ "description": "Flexible and feature-complete Redis client for PHP and HHVM",
+ "homepage": "http://github.com/nrk/predis",
+ "keywords": [
+ "nosql",
+ "predis",
+ "redis"
+ ],
+ "time": "2016-06-16 16:22:20"
+ },
+ {
"name": "psr/log",
"version": "1.0.2",
"source": {
View
@@ -174,7 +174,7 @@
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
- // App\Providers\BroadcastServiceProvider::class,
+ App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
@@ -0,0 +1,19 @@
+{
+ "appKey": "p9toimn5fkqgc21qt0tcjqsgp25qh9o3peg9ai2gkrs55q8m218r98phlhiq",
+ "authHost": "http://192.168.33.40",
+ "authEndpoint": "/broadcasting/auth",
+ "database": "redis",
+ "databaseConfig": {
+ "redis": {},
+ "sqlite": {
+ "databasePath": "/database/laravel-echo-server.sqlite"
+ }
+ },
+ "devMode": false,
+ "host": "",
+ "port": "6001",
+ "referrers": [],
+ "socketio": {},
+ "sslCertPath": "",
+ "sslKeyPath": ""
+}
View
@@ -14,5 +14,9 @@
"lodash": "^4.16.2",
"vue": "^2.0.1",
"vue-resource": "^1.0.3"
+ },
+ "dependencies": {
+ "laravel-echo": "^1.1.3",
+ "pusher-js": "^4.0.0"
}
}
@@ -37,9 +37,10 @@ Vue.http.interceptors.push((request, next) => {
* allows your team to easily build robust real-time web applications.
*/
-// import Echo from "laravel-echo"
+import Echo from "laravel-echo"
-// window.Echo = new Echo({
-// broadcaster: 'pusher',
-// key: 'your-pusher-key'
-// });
+window.Echo = new Echo({
+ broadcaster: 'socket.io',
+ host: '192.168.33.40:6001',
+ // namespace: ''
+});
@@ -11,7 +11,7 @@
{{ user.name }}
</option>
</select>
- <input class="form-control" name="text" v-model="text"></input>
+ <input class="form-control" name="text" v-model="text"/>
<button class="btn btn-default" v-on:click="submitMessage">Submit</button>
</div>
</div>
@@ -48,9 +48,21 @@
}
})
+ this.$http.get('/api/me').then((response) => {
+ console.log(response)
+ this.me = response.body
+
+ Echo.private('user.' + this.me.id)
+ .listen('MessageCreatePrivateEvent', (e) => {
+ console.log(e);
+ this.messages.unshift(e.message);
+ });
+ })
+
},
data() {
return {
+ me: null,
selected: '',
options: [],
text: '',
@@ -71,11 +83,6 @@
}).then((response) => {
console.log(response)
this.text = ''
- this.$http.get('/api/messages/with/' + this.selected)
- .then((response) => {
- console.log(response)
- this.messages = response.body
- })
}, (response) => {
console.log(response)
})
@@ -14,6 +14,7 @@
<link href="/css/app.css" rel="stylesheet">
<!-- Scripts -->
+ <script src="//192.168.33.40:6001/socket.io/socket.io.js"></script>
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
View
@@ -19,6 +19,7 @@
Route::get('/home', 'HomeController@index');
+Route::get('/api/me', 'ApiUsersController@me');
Route::get('/api/users', 'ApiUsersController@listUsers');
Route::get('/api/messages/with/{user}', 'ApiMessagesController@listMessages');
Route::post('/api/messages/to/{user}', 'ApiMessagesController@createMessage');

0 comments on commit 2386f62

Please sign in to comment.