Getting Started
ioredis-serverless is a drop-in replacement for ioredis. It uses the same API — the only difference is the transport layer (WebSocket instead of TCP).
For complete API documentation, refer to the official ioredis documentation.
Installation
bash
npm install @bello-kv/ioredis-serverlessOr with other package managers:
bash
yarn add @bello-kv/ioredis-serverless
pnpm add @bello-kv/ioredis-serverlessPrerequisites
ioredis-serverless requires a WebSocket-to-Redis proxy. The proxy must:
- Accept WebSocket connections
- Forward raw Redis protocol data to a Redis server
- Return Redis responses back through the WebSocket
Basic Connection
The only difference to ioredis is passing a WebSocket URL via the url option:
javascript
import Redis from '@bello-kv/ioredis-serverless'
const redis = new Redis({
url: 'wss://your-proxy.example.com'
})
// From here on, everything works exactly like ioredis
await redis.set('foo', 'bar')
const value = await redis.get('foo')
console.log(value) // 'bar'All ioredis features are supported: commands, pipelining, transactions, Lua scripts, pub/sub, cluster mode, etc. See the ioredis documentation for details.
Cloudflare Workers
javascript
import Redis from '@bello-kv/ioredis-serverless'
export default {
async fetch(request, env) {
const redis = new Redis({
url: env.REDIS_PROXY_URL
})
const count = await redis.incr('page-views')
return new Response(`Page views: ${count}`)
}
}Vercel Edge
javascript
import Redis from '@bello-kv/ioredis-serverless'
export const config = { runtime: 'edge' }
export default async function handler(request) {
const redis = new Redis({
url: process.env.REDIS_PROXY_URL
})
const data = await redis.get('cached-data')
return new Response(JSON.stringify({ data }), {
headers: { 'Content-Type': 'application/json' }
})
}Bunny Edge Scripting
javascript
import Redis from '@bello-kv/ioredis-serverless'
export default {
async fetch(request) {
const redis = new Redis({
url: 'wss://your-proxy.example.com'
})
const cached = await redis.get('cached-data')
return new Response(cached || 'No data')
}
}