Skip to content

Core\Net::connectLocal

Connects to a server on the same computer through a socket file. This kind of connection is called a Unix-domain socket. Many local services accept connections this way, such as a database or a cache.

You give the path of the socket file and the longest time the connection may take to open. The result is a Core\Net\Stream, the same as for Core\Net::connect. You call write and read on it, and close when you are done.

The path must be allowed under local in the [capabilities.net] block of nvs.toml. Otherwise the call throws a RuntimeError. When nothing listens at the path, it throws an IOError. When the time runs out, it throws a TimeoutError.

Good to know: socket files work on Linux and other Unix systems. On Windows, this call throws a RuntimeError.

Parameters

  • $pathstringThe absolute path of a socket that listens. It is never read as a host or as a URL. This is the only method that connects to a path.
  • $withinCore\Time\DurationHow long to wait for the connection. This limit is for this call only. A local connection waits only when the listener's queue of waiting connections is full.

Return value

Returns Core\Net\Stream. A connected Core\Net\Stream. If your program does not close it, it is closed when the request ends.

Errors

  • RuntimeErrorThe configuration does not allow this path in net.local. It is also thrown when this build has no support for Unix-domain sockets, or $within is not a positive length of time.
  • TimeoutErrorThe connection was not open yet when the time in $within ended.
  • IOErrorThe operating system did not allow the path. Nothing listens there, the program has no permission, or the path is too long for the platform.

Examples

The examples below run with this configuration file.

nvs.toml
# The examples start small servers on socket files and connect to them.
# These lines allow only those socket files.
[capabilities.net]
local = ["/tmp/novis-prices.sock", "/tmp/novis-orders.sock", "/tmp/novis-cache.sock"]

Asking a local service

<?nvs
// Shows how a program talks to a service on the same computer through a socket file.
// requires: unix
// This starts a small price service, so the example has something to connect to.
string $path = '/tmp/novis-prices.sock';
Core\Net\Listener $service = Core\Net::listenLocal($path);
// `connectLocal` connects to the socket file. It waits up to 5 seconds.
Core\Net\Stream $client = Core\Net::connectLocal($path, 5s);
$client->write("PRICE coffee\n" as bytes, 5s);
// The service reads the question and sends the answer.
Core\Net\Stream $connection = $service->accept(5s);
tainted string $question = Core\Str::trim($connection->read(64, 5s) as string);
echo 'service got: ', $question, "\n";
$connection->write("3.50\n" as bytes, 5s);
// This prints `client got: 3.50`.
echo 'client got: ', Core\Str::trim($client->read(64, 5s) as string), "\n";
$connection->close();
$client->close();
$service->close();
// The socket file stays on disk after `close`, so the program deletes it.
Core\IO::remove($path);
Output
service got: PRICE coffee
client got: 3.50

A socket file that is not allowed

<?nvs
// Shows what happens when a program connects to a socket file its configuration does not allow.
// The `nvs.toml` beside this example does not allow `/run/docker.sock`.
// So this call throws a `RuntimeError`, and no connection is opened.
try {
Core\Net::connectLocal('/run/docker.sock', 5s);
} catch (RuntimeError $refused) {
echo $refused->message, "\n";
}
Output
Core\Net::connectLocal needs the capability `net.local` for /run/docker.sock, which is not granted
help: add `local = ["/run/docker.sock"]` under `[capabilities.net]` in nvs.toml

Checking local services

<?nvs
// Shows a health check that tests whether two local services are running.
// requires: unix
// Only the orders service is started here. Nothing listens at the cache socket file.
Core\Net\Listener $orders = Core\Net::listenLocal('/tmp/novis-orders.sock');
array<string> $services = ['orders' => '/tmp/novis-orders.sock', 'cache' => '/tmp/novis-cache.sock'];
// `connectLocal` throws an `IOError` when nothing listens at the path.
// This prints `orders: running` and then `cache: not running`.
foreach ($services as string $name => string $path) {
try {
Core\Net\Stream $probe = Core\Net::connectLocal($path, 1s);
$probe->close();
echo $name, ": running\n";
} catch (IOError $down) {
echo $name, ": not running\n";
}
}
$orders->close();
Core\IO::remove('/tmp/novis-orders.sock');
Output
orders: running
cache: not running