dart valkey/redis
This project is a robust, type-safe Dart client for Redis (and Valkey) that manages both command and Pub/Sub interactions. It provides built‐in connection management, automatic reconnection, and pluggable authentication. Thanks to its modular design, the user can easily extend commands as Dart extensions and choose between a secure (TLS) and insecure connection.
Features
-
Connection Management
Uses the Template Method pattern inBaseConnectionto handle the socket connection, reconnection logic, and error management automatically. -
Authentication and Command Execution
Implements authentication commands (e.g.HelloCommandandAuthCommand) to ensure secure and correct data exchange with the Redis/Valkey server. -
Pub/Sub Support
Supports Pub/Sub operations with theValkeySubscriptionClientwhich handles subscriptions to channels and patterns and delivers messages via a unified stream. -
Extensible & Modular
Commands are organized into modules so that new commands can be added as extensions. The core libraries (ValkeyCommandClientandValkeyClient) provide a foundation for building more specialized client implementations.
Installation
-
Add the dependency in your pubspec.yaml:
// filepath: pubspec.yaml dependencies: dart_valkey: any -
Install the dependency by running:
pub get
Usage
Connecting and Sending Commands
To connect and execute commands, first import the client library:
import 'package:dart_valkey/dart_valkey.dart';
Future<void> main() async {
// Create a command client (or use a specific implementation)
final client = ValkeyCommandClient(
host: 'localhost',
port: 6379,
username: 'your-username', // optional
password: 'your-password', // optional
);
// Connect to the server
await client.connect();
// Execute a command (for example, a PING command if implemented)
final response = await client.execute(PingCommand());
print('Server response: $response');
// Close the client when done
await client.close();
}
Pub/Sub Example
Subscribing to channels and listening for messages is simple with the ValkeySubscriptionClient:
import 'package:dart_valkey/dart_valkey.dart';
import 'dart:async';
Future<void> main() async {
final subClient = ValkeySubscriptionClient(
host: 'localhost',
port: 6379,
);
// Connect to the server
await subClient.connect();
// Subscribe to a channel
subClient.subscribe(['channel1']);
// Listen to Pub/Sub messages
final subscription = subClient.messages.listen((PubSubMessage msg) {
print('Received message: ${msg.message} on channel: ${msg.channel}');
});
// Run for a while and cancel subscription when done.
await Future.delayed(const Duration(seconds: 30));
await subClient.close();
await subscription.cancel();
}
Commands Implementation Status
The implementation status of the Valkey commands is shown below, as indicated in the official documentation. Commands marked with [x] are implemented, while those marked with [ ] are not yet implemented.
Bitmap Operations
Bloom filter Operations
Cluster
ASKINGCLUSTER ADDSLOTSCLUSTER ADDSLOTSRANGECLUSTER BUMPEPOCHCLUSTER COUNT-FAILURE-REPORTSCLUSTER COUNTKEYSINSLOTCLUSTER DELSLOTSCLUSTER DELSLOTSRANGECLUSTER FAILOVERCLUSTER FLUSHSLOTSCLUSTER FORGETCLUSTER GETKEYSINSLOTCLUSTER HELPCLUSTER INFOCLUSTER KEYSLOTCLUSTER LINKSCLUSTER MEETCLUSTER MYIDCLUSTER MYSHARDIDCLUSTER NODESCLUSTER REPLICASCLUSTER REPLICATECLUSTER RESETCLUSTER SAVECONFIGCLUSTER SET-CONFIG-EPOCHCLUSTER SETSLOTCLUSTER SHARDSCLUSTER SLAVESCLUSTER SLOT-STATSCLUSTER SLOTSREADONLYREADWRITE
Connection
xAUTHxCLIENT CACHINGxCLIENT CAPAxCLIENT GETNAMExCLIENT GETREDIRxCLIENT HELPxCLIENT IDxCLIENT IMPORT-SOURCExCLIENT INFOxCLIENT KILLxCLIENT LISTxCLIENT NO-EVICTxCLIENT NO-TOUCHxCLIENT PAUSExCLIENT REPLYxCLIENT SETINFOxCLIENT SETNAMExCLIENT TRACKINGxCLIENT TRACKINGINFOxCLIENT UNBLOCKxCLIENT UNPAUSExECHOxHELLOxPINGxQUITxRESETxSELECT
Generic
COPYxDELDUMPxEXISTSxEXPIREEXPIREATEXPIRETIMEKEYSMIGRATEMOVEOBJECT ENCODINGOBJECT FREQOBJECT HELPOBJECT IDLETIMEOBJECT REFCOUNTxPERSISTPEXPIREPEXPIREATPEXPIRETIMEPTTLRANDOMKEYxRENAMExRENAMENXRESTORESCANSORTSORT_ROTOUCHxTTLxTYPEUNLINKWAITWAITAOF
Geospatial indices
GEOADDGEODISTGEOHASHGEOPOSGEORADIUSGEORADIUSBYMEMBERGEORADIUSBYMEMBER_ROGEORADIUS_ROGEOSEARCHGEOSEARCHSTORE
Hash Operations
xHDELxHEXISTSxHGETxHGETALLxHINCRBYxHINCRBYFLOATxHKEYSxHLENxHMGETxHMSETHRANDFIELDHSCANxHSETxHSETNXxHSTRLENxHVALS
HyperLogLog
JSON Operations
JSON.ARRAPPENDJSON.ARRINDEXJSON.ARRINSERTJSON.ARRLENJSON.ARRPOPJSON.ARRTRIMJSON.CLEARJSON.DEBUGJSON.DELJSON.FORGETJSON.GETJSON.MGETJSON.MSETJSON.NUMINCRBYJSON.NUMMULTBYJSON.OBJKEYSJSON.OBJLENJSON.RESPJSON.SETJSON.STRAPPENDJSON.STRLENJSON.TOGGLEJSON.TYPE
List Operations
BLMOVEBLMPOPBLPOPBRPOPBRPOPLPUSHxLINDEXxLINSERTxLLENLMOVELMPOPxLPOPLPOSxLPUSHLPUSHXxLRANGExLREMLSETxLTRIMxRPOPxRPOPLPUSHxRPUSHRPUSHX
Pub/Sub
xPSUBSCRIBExPUBLISHxPUNSUBSCRIBExPUBSUB CHANNELSxPUBSUB HELPxPUBSUB NUMPATxPUBSUB NUMSUBxPUBSUB SHARDCHANNELSxPUBSUB SHARDNUMSUBxSPUBLISHxSSUBSCRIBExSUBSCRIBExSUNSUBSCRIBExUNSUBSCRIBE
Scripting and Functions
EVALEVALSHAEVALSHA_ROEVAL_ROFCALLFCALL_ROFUNCTION DELETEFUNCTION DUMPFUNCTION FLUSHFUNCTION HELPFUNCTION KILLFUNCTION LISTFUNCTION LOADFUNCTION RESTOREFUNCTION STATSSCRIPT DEBUGSCRIPT EXISTSSCRIPT FLUSHSCRIPT HELPSCRIPT KILLSCRIPT LOADSCRIPT SHOW
Search
Server
ACL CATACL DELUSERACL DRYRUNACL GENPASSACL GETUSERACL HELPACL LISTACL LOADACL LOGACL SAVEACL SETUSERACL USERSACL WHOAMIBGREWRITEAOFBGSAVECOMMAND COUNTCOMMAND DOCSCOMMAND GETKEYSCOMMAND GETKEYSANDFLAGSCOMMAND HELPCOMMAND INFOCOMMAND LISTCOMMANDLOG GETCOMMANDLOG HELPCOMMANDLOG LENCOMMANDLOG RESETCONFIG GETCONFIG HELPCONFIG RESETSTATCONFIG REWRITECONFIG SETDBSIZEDEBUGFAILOVERFLUSHALLFLUSHDBINFOLASTSAVELATENCY DOCTORLATENCY GRAPHLATENCY HELPLATENCY HISTOGRAMLATENCY HISTORYLATENCY LATESTLATENCY RESETLOLWUTMEMORY DOCTORMEMORY HELPMEMORY MALLOC-STATSMEMORY PURGEMEMORY STATSMEMORY USAGEMODULE HELPMODULE LISTMODULE LOADMODULE LOADEXMODULE UNLOADMONITORPSYNCREPLCONFREPLICAOFRESTORE-ASKINGROLESAVESHUTDOWNSLAVEOFSLOWLOG GETSLOWLOG HELPSLOWLOG LENSLOWLOG RESETSWAPDBSYNCTIME
Set Operations
xSADDxSCARDxSDIFFxSDIFFSTORExSINTERSINTERCARDxSINTERSTORExSISMEMBERxSMEMBERSSMISMEMBERxSMOVExSPOPxSRANDMEMBERxSREMSSCANxSUNIONxSUNIONSTORE
Sorted Set Operations
xZADDBZMPOPBZPOPMAXBZPOPMINxZCARDxZCOUNTZDIFFZDIFFSTORExZINCRBYZINTERZINTERCARDZINTERSTOREZLEXCOUNTZMPOPZMSCOREZPOPMAXZPOPMINZRANDMEMBERxZRANGEZRANGEBYLEXxZRANGEBYSCOREZRANGESTORExZRANKxZREMZREMRANGEBYLEXZREMRANGEBYRANKZREMRANGEBYSCORExZREVRANGEZREVRANGEBYLEXxZREVRANGEBYSCORExZREVRANKZSCANxZSCOREZUNIONZUNIONSTORE
Stream Operations
XACKXADDXAUTOCLAIMXCLAIMXDELXGROUP CREATEXGROUP CREATECONSUMERXGROUP DELCONSUMERXGROUP DESTROYXGROUP HELPXGROUP SETIDXINFO CONSUMERSXINFO GROUPSXINFO HELPXINFO STREAMXLENXPENDINGXRANGEXREADXREADGROUPXREVRANGEXSETIDXTRIM
String Operations
xAPPENDBITCOUNTBITFIELDxDECRxDECRBYDELIFEQxGETGETBITGETDELGETEXxGETRANGExGETSETxINCRxINCRBYINCRBYFLOATLCSxMGETxMSETMSETNXPSETEXxSETSETBITSETEXSETNXxSETRANGExSTRLENSUBSTR
Transactions
Contributing
Contributions are welcome!
Feel free to open issues or submit pull requests on our repository.