-
-
Save enijar/9c02eba4e9768d035b17 to your computer and use it in GitHub Desktop.
Socket.io Web Sockets with Laravel 5.1 & Redis
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
// Remember to include the socket.io.js script - https://cdn.socket.io/socket.io-1.3.7.js | |
// Connect to the server | |
var socket = io.connect('http://james.local:8080'); | |
// Listen to events | |
socket.on('FinerVision:connected', function (data) { | |
// Emit an event to the server | |
socket.emit('some.event', 'Some data'); | |
console.log(data); | |
}); |
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
{ | |
"name": "laravel/laravel", | |
"description": "The Laravel Framework.", | |
"keywords": [ | |
"framework", | |
"laravel" | |
], | |
"license": "MIT", | |
"type": "project", | |
"require": { | |
"php": ">=5.5.9", | |
"laravel/framework": "5.1.*", | |
"predis/predis": "^1.1@dev" | |
}, | |
"require-dev": { | |
"fzaninotto/faker": "~1.4", | |
"mockery/mockery": "0.9.*", | |
"phpunit/phpunit": "~4.0", | |
"phpspec/phpspec": "~2.1" | |
}, | |
"autoload": { | |
"classmap": [ | |
"database" | |
], | |
"psr-4": { | |
"App\\": "app/" | |
} | |
}, | |
"autoload-dev": { | |
"classmap": [ | |
"tests/TestCase.php" | |
] | |
}, | |
"scripts": { | |
"post-install-cmd": [ | |
"php artisan clear-compiled", | |
"php artisan optimize" | |
], | |
"pre-update-cmd": [ | |
"php artisan clear-compiled" | |
], | |
"post-update-cmd": [ | |
"php artisan optimize" | |
], | |
"post-root-package-install": [ | |
"php -r \"copy('.env.example', '.env');\"" | |
], | |
"post-create-project-cmd": [ | |
"php artisan key:generate" | |
] | |
}, | |
"config": { | |
"preferred-install": "dist" | |
} | |
} |
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, | |
"devDependencies": { | |
"gulp": "^3.8.8" | |
}, | |
"dependencies": { | |
"bootstrap-sass": "^3.0.0", | |
"http": "0.0.0", | |
"ioredis": "^1.10.0", | |
"laravel-elixir": "^3.0.0", | |
"socket.io": "^1.3.7" | |
} | |
} |
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 server = require('http').Server(); | |
var io = require('socket.io')(server); | |
var Redis = require('ioredis'); | |
var redis = new Redis(); | |
redis.subscribe('FinerVision'); | |
redis.on('message', function (channel, message) { | |
message = JSON.parse(message); | |
io.emit(channel + ':' + message.event, message.data); | |
console.log(channel + ':' + message.event, message.data); | |
}); | |
server.listen(8080); |
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 Illuminate\Http\Request; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
class SocketController extends Controller | |
{ | |
private $channel = 'FinerVision'; | |
public function index() | |
{ | |
$data = [ | |
'event' => 'connected', | |
'data' => [ | |
'status' => 'connected', | |
], | |
]; | |
$this->publish($data); | |
return view('index'); | |
} | |
private function publish($data) | |
{ | |
\Redis::publish($this->channel, json_encode($data)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Steps
Install Dependencies for Node and Laravel
Start Redis Server
Check if the server is running with (should see PONG as output)
Start the server with