redis mock
real-time infrastructure
sse
pnpm
chime
// consumer
// Create EventSource for SSE connection
sse = new EventSource('/api/events');
setEventSource(sse);
// Handle connection established
sse.addEventListener('connected', (event) => {
console.log('SSE connection established successfully');
setError(null); // Clear any previous errors
});
// Handle initial trades batch
sse.addEventListener('trades', (event) => {
...
}
// sse endpoint - stream producer
export async function GET(req: NextRequest) {
// Create a stream response
const encoder = new TextEncoder();
const stream = new ReadableStream({
async start(controller) {
// Send initial connection established message
controller.enqueue(encoder.encode(createSSEMessage({ connected: true }, 'connected')));
...
}
})
// Return the response with appropriate headers
return new Response(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-transform',
'Connection': 'keep-alive',
'X-Accel-Buffering': 'no', // Disable Nginx buffering
},
});
}
// Simple in-memory implementation of Redis for testing
// This avoids the Node.js compatibility issues with redis-mock
// Storage
const keyValueStore: Record<string, string> = {};
const listStore: Record<string, string[]> = {};
const subscribers: Record<string, Array<(message: string) => void>> = {};
...
// Export the Redis mock interface
export const redis = {
// Basic operations
get: async (key: string) => {
console.log(`[REDIS MOCK] GET ${key}`);
return keyValueStore[key] || null;
},
set: async (key: string, value: string) => {
console.log(`[REDIS MOCK] SET ${key}`);
keyValueStore[key] = value;
return 'OK';
},
del: async (key: string) => {
console.log(`[REDIS MOCK] DEL ${key}`);
const existed = key in keyValueStore;
delete keyValueStore[key];
return existed ? 1 : 0;
},
// List operations
lpush: async (key: string, value: string) => {
console.log(`[REDIS MOCK] LPUSH ${key}`);
if (!listStore[key]) {
listStore[key] = [];
}
listStore[key].unshift(value);
return listStore[key].length;
},
rpush: async (key: string, value: string) => {
console.log(`[REDIS MOCK] RPUSH ${key}`);
if (!listStore[key]) {
listStore[key] = [];
}
listStore[key].push(value);
return listStore[key].length;
},
lrange: async (key: string, start: number, end: number) => {
console.log(`[REDIS MOCK] LRANGE ${key} ${start} ${end}`);
if (!listStore[key]) {
return [];
}
// Handle negative indices like Redis does
const len = listStore[key].length;
const adjustedStart = start < 0 ? len + start : start;
const adjustedEnd = end < 0 ? len + end : end;
return listStore[key].slice(adjustedStart, adjustedEnd + 1);
},
ltrim: async (key: string, start: number, end: number) => {
console.log(`[REDIS MOCK] LTRIM ${key} ${start} ${end}`);
if (!listStore[key]) {
return 'OK';
}
// Handle negative indices like Redis does
const len = listStore[key].length;
const adjustedStart = start < 0 ? len + start : start;
const adjustedEnd = end < 0 ? len + end : end;
listStore[key] = listStore[key].slice(adjustedStart, adjustedEnd + 1);
return 'OK';
},
// Pub/Sub operations
publish: async (channel: string, message: string) => {
console.log(`[REDIS MOCK] PUBLISH ${channel}`);
// Notify subscribers
if (subscribers[channel]) {
subscribers[channel].forEach(callback => {
try {
callback(message);
} catch (err) {
console.error(`[REDIS MOCK] Error in subscriber callback:`, err);
}
});
}
return subscribers[channel]?.length || 0;
},
subscribe: async (channel: string, callback: (message: string) => void) => {
console.log(`[REDIS MOCK] SUBSCRIBE ${channel}`);
if (!subscribers[channel]) {
subscribers[channel] = [];
}
subscribers[channel].push(callback);
// Return a mock subscriber
return {
unsubscribe: async () => {
console.log(`[REDIS MOCK] UNSUBSCRIBE ${channel}`);
const index = subscribers[channel]?.indexOf(callback) ?? -1;
if (index !== -1) {
subscribers[channel].splice(index, 1);
}
return true;
},
quit: async () => {
console.log(`[REDIS MOCK] QUIT subscriber`);
return true;
}
};
}
};
packageManager
in package.json
cursor suggest running commands with npm
so maybe adding a cursor rule might help - haven't tested this yet
Check out the latest insights from @3070.eth on creating a simple Redis mock for testing that can help streamline your real-time infrastructure projects. This post covers key operations like GET, SET, and Pub/Sub functions. It's a must-read for developers working with node environments!