-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1292420
commit c425356
Showing
12 changed files
with
107 additions
and
114 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
17 changes: 6 additions & 11 deletions
17
chat-service/src/infrastructure/kafka/consumers/createGroupChat.ts
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 |
---|---|---|
@@ -1,12 +1,7 @@ | ||
import { createChat } from "../../database/mongoDB/repositories" | ||
import { createChat } from "../../database/mongoDB/repositories"; | ||
|
||
|
||
export default async( | ||
data:any | ||
)=>{ | ||
try { | ||
await createChat(data) | ||
} catch (error) { | ||
|
||
} | ||
} | ||
export default async (data: any) => { | ||
try { | ||
await createChat(data); | ||
} catch (error) {} | ||
}; |
19 changes: 7 additions & 12 deletions
19
chat-service/src/infrastructure/kafka/consumers/individualChatCreationConsumer.ts
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 |
---|---|---|
@@ -1,14 +1,9 @@ | ||
import { createChat } from "../../database/mongoDB/repositories"; | ||
|
||
|
||
export default async( | ||
data:any | ||
)=>{ | ||
try { | ||
await createChat(data) | ||
} catch (error) { | ||
console.log(error); | ||
|
||
} | ||
} | ||
|
||
export default async (data: any) => { | ||
try { | ||
await createChat(data); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,25 +1,26 @@ | ||
import { Consumer, Kafka, Partitioners, Producer } from "kafkajs" | ||
import { Consumer, Kafka, Partitioners, Producer } from "kafkajs"; | ||
|
||
const kafka = new Kafka({ | ||
clientId: 'chat-service', | ||
brokers: ['pkc-4j8dq.southeastasia.azure.confluent.cloud:9092'], | ||
ssl: true, | ||
sasl: { | ||
mechanism: 'plain', | ||
username: "AH3AIXDBMRITWY2S", | ||
password: 'PTTwXBptxZjyOa3DLtmSIjgC3mg8AZG8o1MB0pShQvbNX7bTC07O8HcgLAi+sqUj', | ||
}, | ||
connectionTimeout: 30000, | ||
authenticationTimeout: 30000, | ||
}); | ||
clientId: "chat-service", | ||
brokers: ["pkc-4j8dq.southeastasia.azure.confluent.cloud:9092"], | ||
ssl: true, | ||
sasl: { | ||
mechanism: "plain", | ||
username: "IGI4TMEEZDD5XDZG", | ||
password: "KYygx3UkksOGC9+Iur1t5EPU3MlyQfY2qBgJ1zHfxW3leYtYefDoikTYcR8EjsPk", | ||
}, | ||
connectionTimeout: 30000, | ||
authenticationTimeout: 30000, | ||
}); | ||
|
||
// export const kafka =new Kafka({ | ||
// clientId:'chat-service', | ||
// brokers:['34.93.145.38:29092'] | ||
// }) | ||
|
||
export const producer:Producer = kafka.producer({ createPartitioner: Partitioners.LegacyPartitioner }); | ||
export const consumer:Consumer=kafka.consumer({ | ||
groupId:'chat-service-kafka-group' | ||
}) | ||
|
||
export const producer: Producer = kafka.producer({ | ||
createPartitioner: Partitioners.LegacyPartitioner, | ||
}); | ||
export const consumer: Consumer = kafka.consumer({ | ||
groupId: "chat-service-kafka-group", | ||
}); |
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 |
---|---|---|
@@ -1,56 +1,65 @@ | ||
import { consumer } from "../infrastructure/kafka"; | ||
import { INotificationSubscriber, createSubscriber } from "../infrastructure/kafka"; | ||
import { | ||
INotificationSubscriber, | ||
createSubscriber, | ||
} from "../infrastructure/kafka"; | ||
|
||
export const runConsumer = async () => { | ||
try { | ||
await consumer.connect(); | ||
await consumer.subscribe({ topic: "notification-service-topic", fromBeginning: true }); | ||
|
||
const subscriber = createSubscriber(); | ||
|
||
await consumer.run({ | ||
eachMessage: async ({ message }) => { | ||
const { key, value } = message; | ||
|
||
if (!key) { | ||
console.error("Message key is missing."); | ||
return; | ||
} | ||
if (!value) { | ||
console.error("Message value is missing."); | ||
return; | ||
} | ||
|
||
const subscriberMethod = convertKeyToMethodName(String(key));; | ||
|
||
if (typeof subscriber[subscriberMethod] !== 'function') { | ||
console.error(`Method ${subscriberMethod} is not defined on subscriber.`); | ||
return; | ||
} | ||
|
||
try { | ||
const subscriberData = JSON.parse(String(value)); | ||
await subscriber[subscriberMethod](subscriberData); | ||
} catch (error: any) { | ||
console.error(`Error processing message with key ${key}: ${error.message}`); | ||
} | ||
} | ||
}); | ||
|
||
} catch (error: any) { | ||
console.error("Kafka Consume Error -> Notification : ", error.message); | ||
} | ||
} | ||
try { | ||
await consumer.connect(); | ||
await consumer.subscribe({ | ||
topic: "notification-service-topic", | ||
fromBeginning: true, | ||
}); | ||
|
||
const subscriber = createSubscriber(); | ||
|
||
await consumer.run({ | ||
eachMessage: async ({ message }) => { | ||
const { key, value } = message; | ||
|
||
if (!key) { | ||
console.error("Message key is missing."); | ||
return; | ||
} | ||
if (!value) { | ||
console.error("Message value is missing."); | ||
return; | ||
} | ||
|
||
const subscriberMethod = convertKeyToMethodName(String(key)); | ||
|
||
if (typeof subscriber[subscriberMethod] !== "function") { | ||
console.error( | ||
`Method ${subscriberMethod} is not defined on subscriber.` | ||
); | ||
return; | ||
} | ||
|
||
try { | ||
const subscriberData = JSON.parse(String(value)); | ||
await subscriber[subscriberMethod](subscriberData); | ||
} catch (error: any) { | ||
console.error( | ||
`Error processing message with key ${key}: ${error.message}` | ||
); | ||
} | ||
}, | ||
}); | ||
} catch (error: any) { | ||
console.error("Kafka Consume Error -> Notification : ", error.message); | ||
} | ||
}; | ||
|
||
function convertKeyToMethodName(key: string): keyof INotificationSubscriber { | ||
const keyMap: { [key: string]: keyof INotificationSubscriber } = { | ||
USER_CREATED_MESSAGE: 'userCreated', | ||
REQUEST_FORGOT_PASSWORD_MESSAGE: 'requestForgotPassword' | ||
}; | ||
return keyMap[key] || key; | ||
const keyMap: { [key: string]: keyof INotificationSubscriber } = { | ||
USER_CREATED_MESSAGE: "userCreated", | ||
REQUEST_FORGOT_PASSWORD_MESSAGE: "requestForgotPassword", | ||
}; | ||
return keyMap[key] || key; | ||
} | ||
|
||
export const stopConsumer = async () => { | ||
await consumer.stop(); | ||
await consumer.disconnect(); | ||
} | ||
await consumer.stop(); | ||
await consumer.disconnect(); | ||
}; |
20 changes: 7 additions & 13 deletions
20
notification-service/src/infrastructure/kafka/consumers/userCreatedConsumer.ts
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 |
---|---|---|
@@ -1,15 +1,9 @@ | ||
import { sendVerificationMail } from "../../../infrastructure/services"; | ||
|
||
export default async ( | ||
data:string | ||
) => { | ||
|
||
try { | ||
|
||
await sendVerificationMail(data); | ||
|
||
} catch (error: any) { | ||
console.log("user-created-consumed mail send error: ", error?.message); | ||
} | ||
|
||
} | ||
export default async (data: string) => { | ||
try { | ||
await sendVerificationMail(data); | ||
} catch (error: any) { | ||
console.log("user-created-consumed mail send error: ", error?.message); | ||
} | ||
}; |
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
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