Getting started with Valkey using JavaScript
Run existing Redis apps with Valkey and learn how to use it with LangChain

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import redis from 'redis';
const client = redis.createClient();
const channelName = 'valkey-channel';
(async () => {
try {
await client.connect();
console.log('Connected to Redis server');
await client.subscribe(channelName, (message, channel) => {
console.log(`message "${message}" received from channel "${channel}"`)
});
console.log('Waiting for messages...');
} catch (err) {
console.error('Error:', err);
}
})();
1
docker run --rm -p 6379:637 valkey/valkey
brew install valkey
. You should now be able to use the Valkey CLI (valkey-cli
).1
2
3
4
git clone https://github.com/abhirockzz/valkey-javascript
cd valkey-javascript
npm install
1
node subscriber.js
1
valkey-cli PUBLISH valkey-channel 'hello valkey'
ioredis
with Valkey as well. Lets write a publisher application:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import Redis from 'ioredis';
const redisClient = new Redis();
const channelName = 'valkey-channel';
const message = process.argv[2];
if (!message) {
console.error('Please provide a message to publish.');
process.exit(1);
}
async function publishMessage() {
try {
const receivedCount = await redisClient.publish(channelName, message);
console.log(`Message "${message}" published to channel "${channelName}". Received by ${receivedCount} subscriber(s).`);
} catch (err) {
console.error('Error publishing message:', err);
} finally {
// Close the client connection
await redisClient.quit();
}
}
publishMessage();
1
2
node publisher.js 'hello1'
node publisher.js 'hello2'
iovalkey
is a fork of ioredis
. I made the following changes to port the producer code to use iovalkey
:import Redis from 'ioredis';
- Added
import Redis from 'iovalkey';
- Installed
iovalkey
-npm install iovalkey
1
2
// import Redis from 'ioredis';
import Redis from 'iovalkey';
iovalkey
based publisher, and confirm that the subscriber is able to receive it:1
node publisher.js 'hello from iovalkey'
1
message "hello from iovalkey" received from channel "valkey-channel"
1
npm install langchain
RedisChatMessageHistory
component.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { BedrockChat } from "@langchain/community/chat_models/bedrock";
import { RedisChatMessageHistory } from "@langchain/redis";
import { ConversationChain } from "langchain/chains";
import { BufferMemory } from "langchain/memory";
import prompt from "prompt";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
const chatPrompt = ChatPromptTemplate.fromMessages([
[
"system",
"The following is a friendly conversation between a human and an AI.",
],
new MessagesPlaceholder("chat_history"),
["human", "{input}"],
]);
const memory = new BufferMemory({
chatHistory: new RedisChatMessageHistory({
sessionId: new Date().toISOString(),
sessionTTL: 300,
host: "localhost",
port: 6379,
}),
returnMessages: true,
memoryKey: "chat_history",
});
const model = "anthropic.claude-3-sonnet-20240229-v1:0"
const region = "us-east-1"
const langchainBedrockChatModel = new BedrockChat({
model: model,
region: region,
modelKwargs: {
anthropic_version: "bedrock-2023-05-31",
},
});
const chain = new ConversationChain({
llm: langchainBedrockChatModel,
memory: memory,
prompt: chatPrompt,
});
while (true) {
prompt.start({noHandleSIGINT: true});
const {message} = await prompt.get(['message']);
const response = await chain.invoke({
input: message,
});
console.log(response);
}
1
node chat.js

List
:1
2
valkey-cli keys *
valkey-cli LRANGE <enter list name> 0 -1

node-redis
client, but I wanted to try out iovalkey
client. I am not a JS/TS expert, but it was simple enough to port the existing implementation.You can refer to the code on GitHub- Comment out
import { RedisChatMessageHistory } from "@langchain/redis";
- Add
import { ValkeyChatMessageHistory } from "./valkey_chat_history.js";
- Replace
RedisChatMessageHistory
withValkeyChatMessageHistory
(while creating thememory
instance)
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.