You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the kafka consumer and i am using socket io for frontend backend connection I want the sender_id in html code to emit in my node code before the consumer gets up ,The code is working properly i just want the value so that i can assign it as my topic in consumer code Kafka node code
var express = require("express");
var app = express();
app.use(require("cors")());
var server = app.listen(3001, () => {
console.log("app started on port 3001");
});
io.on("connection", function (socket) {
socket.on("event", function (data) {
console.log("A client sent us this dumb mesage:", data.message);
console.log(typeof data);
});
});
const { Kafka } = require("kafkajs");
const kafka = new Kafka({
brokers: ["13.232.99.5:9092"],
});
const consumer = kafka.consumer({ groupId: "10" });
const run = async () => {
// Consuming0
await consumer.connect();
await consumer.subscribe({
topic: "2857965457628909",
fromBeginning: true,
});
app.use(express.static("public"));
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
console.log({
partition,
offset: message.offset,
value: message.value.toString(),
});
callSockets(io, message.value.toString());
},
});
};
run().catch(console.error);
function callSockets(io, message) {
io.sockets.emit("update", message);
}
HTML CODE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body id="overview">
<div id="div"></div>
<input id="input" type="text">
<button type="submit" onclick="getDetails(); getClear();">Submit</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.1.2/socket.io.js"></script>
<script>
var socket = io('http://localhost:3001');
var sender_id="123"
socket.emit('message', { data: sender_id });
socket.on('update',function(data){
var role = JSON.parse(data);
var role = JSON.parse(data);
if (role.event == "user") {
//document.getElementById("div").innerHTML=role.text;
var div = document.getElementById('div');
const br = document.createElement("br");
div.appendChild(br)
div.innerHTML += role.text;
}
socket.emit('event',{message:data.toString()});
})
function getDetails() {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({ "event": "bot", "text": `${getInputValue()}`, "sender_id": "2857965457628909", "assigned_to": "14bac85917f4402f841a4a5cc6c41a0e", "agent_name": "un" });
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://dev.api.dolphinchat.ai/bot/v1/produce_message", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
var txt=JSON.parse(raw)
if(txt.event=="bot")
{
console.log(getInputValue())
}
}
function getClear() {
document.getElementById('input').value='';
}
function getInputValue() {
// Selecting the input element and get its value
var inputVal = document.getElementById("input").value;
// Displaying the value
return inputVal;
}
</script>
</body>
</html>
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This is the kafka consumer and i am using socket io for frontend backend connection I want the sender_id in html code to emit in my node code before the consumer gets up ,The code is working properly i just want the value so that i can assign it as my topic in consumer code
Kafka node code
HTML CODE
Beta Was this translation helpful? Give feedback.
All reactions