rybakit/msgpack
Pure-PHP MessagePack serializer/deserializer. Fully compliant with the spec, supports streaming unpacking, unsigned 64-bit integers, custom types/extensions (including timestamps), and object serialization. Well-tested and relatively fast.
Binary Serialization Use Case: The package excels in scenarios requiring high-performance binary serialization (e.g., caching, inter-process communication, or database storage) where JSON’s verbosity is inefficient. Laravel’s built-in JSON serialization (via json_encode) is ~4x larger than MessagePack for equivalent data, making this a strong fit for:
Laravel Integration Points:
json_encode in Cache::put() with MessagePack::pack().RedisQueue or DatabaseQueue).serialize() in Eloquent attributes or Model::toArray().Anti-Patterns:
Laravel Ecosystem Compatibility:
msgpack PECL).composer require rybakit/msgpack.MessagePackMiddleware for API responses.Packer/Unpacker as singletons in Laravel’s IoC container.Model to use MessagePack for attributes or relations serialization.Migration Path:
assertJson() with custom assertMessagePack() in PHPUnit.Performance Gains:
[1,2,3] → 0x93010203 vs '[1,2,3]').json_encode/json_decode (benchmarks in README).| Risk Area | Mitigation Strategy |
|---|---|
| Breaking Changes | Monitor releases for BC breaks. |
| 64-bit Integer Handling | Configure UnpackOptions::BIGINT_AS_STR to avoid PHP integer overflows. |
| Streaming Unpacking | Test with chunked data (e.g., HTTP streaming, Kafka consumers). |
| Custom Types | Extend Extension interface for domain-specific types (e.g., Carbon, Uuid). |
| Error Handling | Catch PackingFailedException/UnpackingFailedException gracefully. |
| Tooling Support | Use msgpack-cli for debugging serialized data (install via npm install -g msgpack-cli). |
spatie/array-to-object) that conflict with MessagePack’s type handling?bytea, Redis raw values)?| Laravel Component | Integration Strategy |
|---|---|
| Caching | Replace json_encode in Illuminate\Cache\Store implementations (e.g., Redis, Database). |
| Sessions | Extend Illuminate\Session\Store to use MessagePack for serialization. |
| Queues | Modify Illuminate\Queue\Serializers\JsonSerializer to support MessagePack. |
| API Responses | Create middleware to compress responses (e.g., Accept: application/msgpack). |
| Eloquent | Extend Illuminate\Database\Eloquent\Model to use MessagePack for attributes/relations. |
| Broadcasting | Replace JSON in Illuminate\Broadcasting\BroadcastManager for WebSocket payloads. |
| Filesystem | Use MessagePack for storing large binary data (e.g., Illuminate\Filesystem\Filesystem). |
Phase 1: Caching & Queues
json_encode calls in Cache::put() and queue payloads.// Before
Cache::put('key', $data, $seconds);
// After
Cache::put('key', MessagePack::pack($data), $seconds);
Phase 2: API Responses
application/msgpack:
public function handle($request, Closure $next) {
if ($request->wantsMsgPack()) {
$response = $next($request);
return $response->setContent(MessagePack::pack($response->getData()));
}
return $next($request);
}
Phase 3: Database & Eloquent
$model->setAttribute('metadata', MessagePack::pack($model->metadata));
Phase 4: Real-Time Systems
GMP/Decimal needed for BIGINT_AS_GMP/BIGINT_AS_DEC.config('app.use_msgpack')).json_encode (e.g., spatie/laravel-medialibrary).msgpack-cli for validation).application/msgpack support.json_last_error()). Use msgpack-cli or write helper functions.InsufficientDataException).How can I help you explore Laravel packages today?