Skip to main content

Connect with NodeJS

Connect to the Aiven for Valkey™ service using NodeJS with the ioredis library.

Variables

Replace placeholders in the code sample with values from your service overview page:

VariableDescription
SERVICE_URIURI for the Aiven for Valkey service connection

Prerequisites

Install the ioredis library using the following command:

npm install --save ioredis

Set up and run

  1. Create a file named index.js and insert the code below, substituting the placeholder with your Aiven for Valkey URI:

    const Valkey = require('ioredis');
    const serviceUri = 'SERVICE_URI';
    const valkey = new Valkey(serviceUri);

    valkey.set('key', 'hello world');

    valkey.get('key').then(function (result) {
    console.log(`The value of key is: ${result}`);
    valkey.disconnect();
    });

    This code creates a key named key with the value hello world without an expiration. It then retrieves this key from the Valkey service and outputs its value.

  2. Run the script using the following command:

    node index.js

    A successful connection displays:

    The value of key is: hello world