Skip to content

Instantly share code, notes, and snippets.

@enijar
Last active August 14, 2017 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save enijar/9c02eba4e9768d035b17 to your computer and use it in GitHub Desktop.
Save enijar/9c02eba4e9768d035b17 to your computer and use it in GitHub Desktop.
Socket.io Web Sockets with Laravel 5.1 & Redis
// 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);
});
{
"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"
}
}
{
"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"
}
}
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);
<?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));
}
}
@enijar
Copy link
Author

enijar commented Nov 7, 2015

Steps

Install Dependencies for Node and Laravel

$  npm install --save http ioredis socket.io
$  composer require predis/predis

Start Redis Server
Check if the server is running with (should see PONG as output)

$  redis-cli ping

Start the server with

$  redis-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment