Created
December 21, 2017 09:53
-
-
Save germanow/4d0616d99d1b215caa8834363bdb2f41 to your computer and use it in GitHub Desktop.
socket.io + redis + laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var socket = io('http://192.168.10.10:3000'); | |
socket.on("message-channel:App\\Events\\MessageSent", function(message){ | |
console.log('listening'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Message; | |
use App\Events\MessageSent; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
class MessagesController extends Controller | |
{ | |
public function index() | |
{ | |
$messages = Message::all(); | |
return view('messages.index', compact('messages')); | |
} | |
public function store(Request $request) | |
{ | |
$message = Message::create($request->all()); | |
broadcast(new MessageSent($message)); | |
return $message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 MessageSent 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 [ | |
'message-channel' | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"private": true, | |
"scripts": { | |
"prod": "gulp --production", | |
"dev": "gulp watch" | |
}, | |
"devDependencies": { | |
"bootstrap-sass": "^3.3.7", | |
"gulp": "^3.9.1", | |
"jquery": "^3.1.0", | |
"laravel-elixir": "^6.0.0-9", | |
"laravel-elixir-browserify-official": "^0.1.3", | |
"laravel-elixir-vueify": "^2.0.0", | |
"lodash": "^4.14.0", | |
"vue": "^1.0.26", | |
"vue-resource": "^0.9.3" | |
}, | |
"dependencies": { | |
"express": "^4.14.0", | |
"ioredis": "^2.3.0", | |
"laravel-echo": "^1.0.5", | |
"pusher-js": "^3.2.1", | |
"socket.io": "^1.4.8" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('express')(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
var Redis = require('ioredis'); | |
var redis = new Redis(); | |
redis.subscribe('message-channel', function(err, count) { | |
}); | |
redis.on('message', function(channel, message) { | |
console.log('Message Received: ' + message); | |
message = JSON.parse(message); | |
io.emit(channel + ':' + message.event, message.data); | |
}); | |
http.listen(3000, function(){ | |
console.log('Listening on Port 3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment