-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement Kafka-based chat system and group management API (WIP)
This commit introduces a Kafka-based chat system and adds group management functionality: - Add KafkaManager class for handling Kafka producer and consumer operations - Implement WebSocket endpoint for real-time chat communication - Integrate Kafka message broadcasting with WebSocket connections - Add API endpoints for: - Retrieving missed messages - Creating chat groups - Joining existing chat groups - Update FastAPI app to use CORS middleware and lifespan management - Implement GroupManagementService and MissedMessageService integration These changes establish the foundation for a scalable, real-time chat system with group management capabilities, leveraging Kafka for message persistence and distribution.
- Loading branch information
1 parent
b035cb6
commit 28a68ea
Showing
3 changed files
with
118 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from aiokafka import AIOKafkaProducer, AIOKafkaConsumer | ||
import json | ||
import asyncio | ||
|
||
class KafkaManager: | ||
def __init__(self, bootstrap_servers, topic): | ||
self.bootstrap_servers = bootstrap_servers | ||
self.topic = topic | ||
self.producer = None | ||
self.consumer = None | ||
|
||
async def start(self): | ||
self.producer = AIOKafkaProducer(bootstrap_servers=self.bootstrap_servers) | ||
self.consumer = AIOKafkaConsumer( | ||
self.topic, | ||
bootstrap_servers=self.bootstrap_servers, | ||
) | ||
await self.producer.start() | ||
await self.consumer.start() | ||
|
||
async def stop(self): | ||
await self.producer.stop() | ||
await self.consumer.stop() | ||
|
||
async def send_message(self, key, value): | ||
await self.producer.send_and_wait(self.topic, key=key.encode('utf-8'), value=json.dumps(value).encode('utf-8')) | ||
|
||
async def consume_messages(self, callback): | ||
async for msg in self.consumer: | ||
message = json.loads(msg.value.decode('utf-8')) | ||
group_id = msg.key.decode('utf-8') | ||
await callback(message, group_id) |