Skip to content

Commit

Permalink
add getKeyValuePairsByPattern, make cors default to []
Browse files Browse the repository at this point in the history
  • Loading branch information
JEMeyer committed Jun 27, 2024
1 parent 42e9191 commit 95f4c7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This server's port (defaults to 3000 if not set)
PORT=

# Cors (optinal - defaults ot *)
# Cors (optional, but defaults to [])
CORS_ORIGIN=

# Port used when communicating to edge servers
Expand Down
20 changes: 19 additions & 1 deletion src/services/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { redis, redisSubscriber } from './redis';
const setupWebSocket = (server: HttpServer) => {
const io = new Server(server, {
cors: {
origin: process.env.CORS_ORIGIN || '*',
origin: process.env.CORS_ORIGIN || [],
methods: ['GET', 'POST'],
credentials: true,
},
Expand Down Expand Up @@ -34,6 +34,24 @@ const setupWebSocket = (server: HttpServer) => {
redis.publish(channelName, message);
});

socket.on('getKeyValuePairsByPattern', async (pattern) => {
try {
const keys = await redis.keys(pattern);
const result: Record<string, string> = {};

for (const key of keys) {
const value = await redis.get(key);
result[key] = value ?? '';
}

console.log('Emitting keyValuePairs:', result); // Debugging log
socket.emit('keyValuePairs', result);
} catch (error) {
console.error('Error fetching key-value pairs:', error);
socket.emit('keyValuePairs', {});
}
});

socket.on('disconnect', () => {
console.log('Client disconnected');
});
Expand Down

0 comments on commit 95f4c7c

Please sign in to comment.