hannesvdvreken/socketio
最新稳定版本:1.0.0-alpha.1
Composer 安装命令:
composer require hannesvdvreken/socketio
包简介
Pushing Socket.io messages to Redis for socket.io's Redis socket manager.
README 文档
README
What does it do?
This package allows you to transfer socket message to a Node.js process via Redis. Socket.io has a redis adapter to allow scaling a Node.js cluster to send socket messages to any client connected to any process. We can use this to send messages straight from PHP.
This package provides a Socketio\Socketio class with similar methods as the socket.io server in Node.js such as
emit, in, to, of and adapter.
Usage
First we can create a Socketio\Socketio object.
$io = new Socketio\Socketio;
It has some of the same methods as in Node.js:
Namespace
$chats = $io->of('/chats'); $io->emit('general', $message); $chats->emit('user.connected', $user);
Setup a server object with a different namespace. The $io object keeps the same namespace.
Or use a callback:
$io->of('/chats', function ($chats) { $chats->emit('user.connected', $user); }); $io->emit('general', $message);
Rooms
Send to specific room(s):
$io->to('room-a')->emit('event', $message); $io->to('room-a')->to('room-b')->emit('event', $message); $io->in('room-c')->emit('event', $message);
Where in is an alias of to. You can send to several rooms at the same time by chaining to calls.
The rooms are reset after every emit call. This is similar behaviour as in Node.js.
Adapters
By default, the Socketio\Socketio object configures a Memory adapter.
This adapter fakes socket message delivery and can be useful for testing/stubbing.
To setup the Redis adapter you need to configure some dependencies first.
$predis = new Predis\Client; $msgpack = new Socketio\Utils\Msgpack; $adapter = new Socketio\Adapters\Redis($predis, $msgpack); $io->adapter($adapter);
If you have ext-msgpack installed, the Socketio\Utils\Msgpack will use the native
msgpack_pack and msgpack_unpack methods which are supposed to be the fastest around.
If not, you can pass it Msgpack\Encoder and Msgpack\Decoder objects to do the encoding/decoding.
These come pre-installed with composer if you require this package.
$msgpack = new Socketio\Utils\Msgpack(new Msgpack\Encoder, new Msgpack\Decoder);
The Node.js process
var app = require('express')(); var socketio = require('socket.io'); var adapter = require('socket.io-redis'); var server = app.listen(process.env.NODE_PORT || 9000); io = socketio(server); io.adapter(adapter());
Right now, you're good to go!
Contributing
Stick to PSR-2 Coding Standards and be nice in communication.
License
hannesvdvreken/socketio 适用场景与选型建议
hannesvdvreken/socketio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 4, 最近一次更新时间为 2014 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hannesvdvreken/socketio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hannesvdvreken/socketio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-12-26