title: How do I connect Kafkajs with Socket.io
body: <p>I've been studying kafkajs and socket.io I'm am very new to it and i cant seem to understand some things.
  I have created a chat application that basically by opening a browser(client) you can type messages and they get displayed in a chat-window.
  I found a tutorial that makes kafka print &quot;this message + i&quot;.
  I want to instead of sending to the topic and printing message+i to print what people type in chat and I'm not sure how I'm supposed to do that.</p>
  <p><strong>This is my consumer.js:</strong></p>
  <pre><code>const { Kafka } = require(&quot;kafkajs&quot;)
  const clientId = &quot;my-app&quot;
  const brokers = [&quot;localhost:9092&quot;]
  const topic = &quot;message-log&quot;
  
  const kafka = new Kafka({ clientId, brokers })
  // create a new consumer from the kafka client, and set its group ID
  // the group ID helps Kafka keep track of the messages that this client
  // is yet to receive
  const consumer = kafka.consumer({ groupId: clientId })
  
  const consume = async () =&gt; {
      // first, we wait for the client to connect and subscribe to the given topic
      await consumer.connect()
      await consumer.subscribe({ topic })
      await consumer.run({
          // this function is called every time the consumer gets a new message
          eachMessage: ({ message }) =&gt; {
              // here, we just log the message to the standard output
              console.log(`received message: ${message.value}`)
          },
      })
  }
  
  module.exports = consume
  </code></pre>
  <p><strong>This is my producer.js:</strong></p>
  <pre><code>// import the `Kafka` instance from the kafkajs library
  const { Kafka } = require(&quot;kafkajs&quot;)
  
  // the client ID lets kafka know who's producing the messages
  const clientId = &quot;my-app&quot;
  // we can define the list of brokers in the cluster
  const brokers = [&quot;localhost:9092&quot;]
  // this is the topic to which we want to write messages
  const topic = &quot;message-log&quot;
  
  // initialize a new kafka client and initialize a producer from it
  const kafka = new Kafka({ clientId, brokers })
  const producer = kafka.producer()
  
  // we define an async function that writes a new message each second
  const produce = async () =&gt; {
      await producer.connect()
      let i = 0
  
      // after the produce has connected, we start an interval timer
      setInterval(async () =&gt; {
          try {
              // send a message to the configured topic with
              // the key and value formed from the current value of `i`
              await producer.send({
                  topic,
                  messages: [
                      {
                          key: String(i),
                          value: &quot;this is message &quot; + i,
                      },
                  ],
              })
  
              // if the message is written successfully, log it and increment `i`
              console.log(&quot;writes: &quot;, i)
              i++
          } catch (err) {
              console.error(&quot;could not write message &quot; + err)
          }
      }, 1000)
  }
  
  module.exports = produce
  </code></pre>
  <p>I know I'm supposed to somehow connect the topics brokers and clients with the socket.io but I'm not sure how.</p>
  <p><strong>Here is my chat.js:</strong></p>
  <pre><code>/* Kane connection sto server opos prin
  exw tin ikanotita na xrhsimopoihsw to io logo tou library pou phra apo to documentation*/
  var socket = io.connect('http://localhost:8000');
  
  // linking Variables toy indexhtml
  var message = document.getElementById('message');
  var username = document.getElementById('username');
  var btn = document.getElementById('send');
  var output = document.getElementById('output');
  var feedback = document.getElementById('feedback');
  
  // Stelnw events pou ginonte apo ton xristi kai stelnonte ston server
  btn.addEventListener('click', function(){
      socket.emit('chat', {
          message: message.value,
          username: username.value
      });
      message.value = &quot;&quot;;
  });
  
  message.addEventListener('keypress', function(){
      socket.emit('typing', username.value);
  })
  
  // Events wste na perimenw to data apo ton server
  socket.on('chat', function(data){
      feedback.innerHTML = '';
      output.innerHTML += '&lt;p&gt;&lt;strong&gt;' + data.username + ': &lt;/strong&gt;' + data.message + '&lt;/p&gt;';
  });
  
  socket.on('typing', function(data){
      feedback.innerHTML = '&lt;p&gt;&lt;em&gt;' + data + ' is typing a message...&lt;/em&gt;&lt;/p&gt;';
  });
  
  </code></pre>