Phase 2 scaffold: mitra online status & pairing logic
Add mitra online/offline status with heartbeat-based auto-offline, customer-mitra pairing via Valkey pub/sub blast, session management, and control center dashboard with real-time stats. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
47
backend/src/plugins/valkey.js
Normal file
47
backend/src/plugins/valkey.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import Redis from 'ioredis'
|
||||
|
||||
let pub
|
||||
let sub
|
||||
let client
|
||||
|
||||
export const getValkeyClient = () => {
|
||||
if (!client) {
|
||||
const url = process.env.VALKEY_URL || 'redis://localhost:6379'
|
||||
client = new Redis(url)
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
export const getValkeyPub = () => {
|
||||
if (!pub) {
|
||||
const url = process.env.VALKEY_URL || 'redis://localhost:6379'
|
||||
pub = new Redis(url)
|
||||
}
|
||||
return pub
|
||||
}
|
||||
|
||||
export const getValkeySub = () => {
|
||||
if (!sub) {
|
||||
const url = process.env.VALKEY_URL || 'redis://localhost:6379'
|
||||
sub = new Redis(url)
|
||||
}
|
||||
return sub
|
||||
}
|
||||
|
||||
export const publish = async (channel, data) => {
|
||||
const pubClient = getValkeyPub()
|
||||
await pubClient.publish(channel, JSON.stringify(data))
|
||||
}
|
||||
|
||||
export const subscribe = (channel, callback) => {
|
||||
const subClient = getValkeySub()
|
||||
subClient.subscribe(channel)
|
||||
subClient.on('message', (ch, message) => {
|
||||
if (ch === channel) {
|
||||
callback(JSON.parse(message))
|
||||
}
|
||||
})
|
||||
return () => {
|
||||
subClient.unsubscribe(channel)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user