Description
Returns PONG if no argument is provided, otherwise returns a copy of the argument as a bulk string. This command is useful for testing if a connection is still alive, or measuring latency.
Method Signature
ping(message?: string): Promise<string | "PONG">
Parameters
Optional message to echo back. If not provided, returns "PONG".
Return Value
"PONG" if no message is provided
- The provided message if one was given
Examples
Basic ping
const response = await redis.ping();
console.log(response); // "PONG"
Ping with message
const response = await redis.ping("Hello Redis!");
console.log(response); // "Hello Redis!"
Test connection
try {
await redis.ping();
console.log("Connection is alive");
} catch (error) {
console.error("Connection failed:", error);
}
Measure latency
const start = Date.now();
await redis.ping();
const latency = Date.now() - start;
console.log(`Latency: ${latency}ms`);
Health check
async function healthCheck() {
try {
const response = await redis.ping();
return response === "PONG";
} catch {
return false;
}
}
const isHealthy = await healthCheck();
console.log(`Server is ${isHealthy ? "healthy" : "down"}`);
See Also
- echo - Echo a message
- time - Get server time