Message Pack for PHP 7.2 - Server & Client implementations

Decebal Dobrica
Mar 14, 2018 · 2 min read
Image for post
Image for post

This package tries to leverage the power of Message Pack and msgpack.php

Mainly this uses https://github.com/msgpack-rpc/msgpack-rpc-php while updating the underlying code & upgrading to the above mentioned msgpack.php package.

There are 3 possible use cases for this repository due to it’s server & client perspectives

  1. Client (my main use case)
$client = new Client('localhost', '1985');  
$messages = $client->call("SyncJob", $job, $payload);
echo array_pop($messages);

aside from the obvious and arguments for the class, let's see the arguments for the call function:

  • first argument is a string naming the function to be called on the server side (see )
  • the method is a variadic function, hence the following arguments are concatenated to an array on the server

2. Server (not thoroughly tested)

try {  
$server = new Server('1985', new App());
echo 'Server is listening on port 1985...';
$server->recv();
} catch (Exception $e) {
echo $e->getMessage();
}

see for full example

3. Client and Server

this would be a good use case if you use multiple services or you need back and forth communication

*if you need this between the same 2 services maybe best to take into account switching to http/2 where

$backChannel = new BackChannel();   
$client = new Client('localhost', '1986', $backChannel);
$messages = $client->call("SyncJob", $job, $payload);
echo array_pop($messages);
try {
$server = new Server('1985', new App(), $backChannel);
echo 'Server is listening on port 1985...';
$server->recv();
} catch (Exception $e) {
echo $e->getMessage();
}

in this case, as it was the case for the original package, it makes sense to use the BackChannel’s combined server and client methods.

Other options

The package provides necessary interfaces & Traits in order to have your own Server or Client enhancements without the need to modify the base classes whom one can mainly use as references rather than implementations as it makes only for the most basic use case.

a few words on the impact of switching to MessagePack from Gearman

I expected the time to first byte to be affected, but given that they both communicate at a low level the benchmarks were almost the same to the byte.

There is a huge change in availability as I expected however, which is solving one big problem for my system.

I am curious to hear what was your experience and how you used it or looking to use it for, please comment below.

Funeral Zone Engineering

Musings, learnings and sharing from the design and…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store