Skip to content

Instantly share code, notes, and snippets.

@ann0see
Last active July 6, 2025 09:13
Show Gist options
  • Save ann0see/e0cd423882ab3d6845786cc3c583f46b to your computer and use it in GitHub Desktop.
Save ann0see/e0cd423882ab3d6845786cc3c583f46b to your computer and use it in GitHub Desktop.
No library example of using JSON-RPC to get access to connected user names of a Jamulus server
<?php
function loadserverstat($rpcsecret, $jsonrpcport) {
// connect to the Jamulus JSON-RPC endpoint
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!socket_connect($socket, "localhost", $jsonrpcport)) {
echo "Could not connect to Jamulus JSON-RPC endpoint. Please check for connectivity errors.";
exit(1);
}
// authenticate
$auth = json_encode(["id" => 1, "jsonrpc"=>"2.0", "method"=>"jamulus/apiAuth", "params"=> ["secret" => $rpcsecret]]) . PHP_EOL;
if (!socket_write($socket, $auth, strlen($auth)) == strlen($auth)) {
echo "Could not start authentication with Jamulus JSON-RPC endpoint";
exit(1);
}
$authResponse = socket_read($socket, 4096, PHP_NORMAL_READ);
if (!$authResponse) {
echo "Could not read authentication from Jamulus JSON-RPC endpoint.";
exit(1);
}
$resp = json_decode($authResponse);
if (isset($resp->result) && $resp->result == "ok") {
// authenticated
// check if Jamulus is in server mode.
$modeRequest = json_encode(["id" => 2, "jsonrpc"=>"2.0", "method"=>"jamulus/getMode", "params"=> ["m" => "g"]]) . PHP_EOL;
if (!socket_write($socket, $modeRequest, strlen($modeRequest)) == strlen($modeRequest)) {
echo "Could not request server or client mode";
exit(1);
}
while ($res = socket_read($socket, 4096, PHP_NORMAL_READ)) {
$encodedRes = json_decode($res);
if (!$encodedRes) {
// failed to decode
error_log("Could not decode string from server");
continue;
}
if ($encodedRes->id === 2 && $encodedRes->result->mode === "server") {
break;
} else {
echo "Jamulus is not running as a server" . PHP_EOL;
exit(1);
}
}
// get the client list
$clientListRequest = json_encode(["id" => 3, "jsonrpc"=>"2.0", "method"=>"jamulusserver/getClients", "params"=> ["m" => "g"]]) . PHP_EOL;
if (!socket_write($socket, $clientListRequest, strlen($clientListRequest)) == strlen($clientListRequest)) {
echo "Could not request client list";
exit(1);
}
// get clients and output as unordered list
while ($res = socket_read($socket, 8192, PHP_NORMAL_READ)) {
// TODO: depending on the response length: bump up the number of bytes to read!
$encodedRes = json_decode($res);
if (!$encodedRes) {
// failed to decode
error_log("Could not decode string from server");
continue;
}
if ($encodedRes->id === 3) {
print "<ul>";
foreach ($encodedRes->result->clients as $client) {
print "<li>" . htmlspecialchars($client->name) . "</li>";
}
print "</ul>" . PHP_EOL;
break;
}
}
} else {
echo "Could not authenticate. Please check your password";
exit(1);
}
}
// TODO: Set port and secrets here.
echo loadserverstat("<yourjsonrpcsecrethere>", <yourjsonrpcporthere>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment