I'm running ReactPHP in a Docker environment to listen for WebSocket messages. I use the port mapping feature like so:
docker run \
--detach \
-p 10002:8081 \
missive-controller
Thus, the external port 10002 maps to 8081 inside the container.
Inside my implementation of MessageComponentInterface, I have this event handler:
public function onMessage(ConnectionInterface $from, $msg)
{
echo sprintf(
"Connection %d sent message \"%s\" to WS server\n",
$from->resourceId,
$msg
);
/* @var $request \Guzzle\Http\Message\EntityEnclosingRequest */
$request = $from->WebSocket->request;
// ... do stuff
}
Now, I am listening to a couple of ports in ReactPHP, in order to differentiate between internet WebSocket requests, and private messages from other containers on the private Docker network. So, to detect the port, I do this (using the Guzzle object set up above):
$request->getPort();
However, that gets me 10002 (the internet port) rather than the internal container-side port (8081). I have a mapping device to look up the association for now, but can I obtain the container port directly?
I am running Ratchet 0.36.
My present answer to this is that it is not possible, at least with the version of Ratchet I am using. I have scanned
$from, and this only seems to know about the port from an external perspective. The only references I can find in aprint_r($from)to the internal port are ones that I have set in a manual mapping array, which does not help.I idly pondered whether this information might be sitting in the
Ratchet\Server\IoServerserver instance, so I injected this into my listener before starting the loop, and examined it withprint_r()again. Same result, I'm afraid; nothing pops out as looking useful.Contrary to the original post, my port mappings are now set up in a
docker-compose.ymlfile, so my solution for now will be to add these mappings in environment vars in the same file, so it is harder for them to come out of sync. I don't wish to hardwire these, as I am using different port mappings depending on environment.It is possible that version 0.4 will support this, so I will revisit when I upgrade.