Mais conteúdo relacionado
Semelhante a laravel websocket(use redis pubsub) [Laravel meetup tokyo] (20)
Mais de Yuuki Takezawa (6)
laravel websocket(use redis pubsub) [Laravel meetup tokyo]
- 5. Laravelを使ってみた感想
Why i chose Laravel?
新しいフレームワークを探していたときにたまたま見つけた
laravel歴 1年
メインで使っていて、もう手放せないくらい
それまでのphpと違う記述方法にほれた
DIがとにかくシンプルかつ強力
世界とつながる強力なコミュニティ
新技術対応がスムーズ(粗結合且つシンプルさ故に)
MVCだけどそうじゃない自由さ(delete model!)
- 9. What is web socket?
双方向通信規格
お互いが情報発信源
持続した接続
リアルタイム性
チャット、お絵描き共有、ブラウザゲームなど
仲間はAjax long polling、HTTP Polling、Silverlightなど
モダンブラウザ以外は非対応
- 14. What is Redis?
NoSQLという分類に入るインメモリ KVS
(RDBMSはもういらない という意味じゃありません)
データの永続性(再起動してもデータあります)
早い、らくちん
(インメモリ故に低コストとは言いがたい)
CPUの1コアを使う
イベントドリブンで即反応
スキーマなどはありません
memcachedのようにsession, cacheもおまかせ
STRINGS, LISTS, SETS, HASHES, SORTED SETS
- 20. Model
model sample
!
class Datastore implements DatastoreInterface{
!
const KEY = "timeline:";
!
/**
* @param array $array
* @return mixed
*/
public function set(array $array)
{
return Redis::connection()->rpush(self::KEY, json_encode($array));
}
!
/**
* @return stdClass
*/
public function get()
{
$array = [];
$result = Redis::connection()->lrange(self::KEY, 0, -1);
if(count($result))
{
array_walk($result, function($value) use(&$array){
$array[] = json_decode($value);
});
return $array;
}
return $result;
}
!
- 21. Controller
controller sample
interfaceをタイプヒンティング(redis以外の何かにするかもしれない!)
!
protected $datastore;
!
/** @param DatastoreInterface $datastore */
public function __construct(DatastoreInterface $datastore)
{
$this->datastore = $datastore;
}
!
public function index()
{
$view = View::make('emit.index');
$view->with('list', $this->datastore->get());
return $view;
}
!
public function store()
{
if($this->datastore->publish(
['body' => Input::get('body', null)]))
{
return Response::json(['result' => true] ,200);
}
}
- 23. Artisan
Artisan sample
実装していきます (SymfonyComponentConsole)
!
public function __construct(AsyncInterface $loop, WampServerInterface $wamp)
{
parent::__construct();
$this->loop = $loop;
$this->wamp = $wamp;
}
!
public function fire()
{
$loop = $this->loop->async();
$this->info('redis subscribe start');
!
$this->pull($loop, $this->wamp);
!
$webSock = new ReactSocketServer($loop);
$webSock->listen($this->option('port'), '0.0.0.0');
!
$webServer = new RatchetServerIoServer(
new RatchetHttpHttpServer(
new RatchetWebSocketWsServer(
new RatchetWampWampServer($this->wamp)
)
), $webSock);
!
$this->info('websocket server boot');
$this->comment('Listening on port ' . $this->option('port'));
$loop->run();
}