Problem with listening to events while integrating with socket.io

I’m using Socket.io to integrate a rasa chatbot in a Next JS website. But I am not able to get the bot_uttered messages.

I used the following code to get the bot up and running whilst following a blog article RASA - Socket.IO integration - DEV Community.

The script tag at the Head of my _app.js file

      <Head>
        <script defer src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.3/socket.io.js"
          integrity="sha512-PU5S6BA03fRv1Q5fpwXjg5nlRrgdoguZ74urFInkbABMCENyx5oP3hrDzYMMPh3qdLdknIvrGj3yqZ4JuU7Nag=="
          crossorigin="anonymous" referrerpolicy="no-referrer"></script>


      </Head>

This is the HTML that I have on the screen

<div id="messages"></div>
    <form id="form">
      <input id="message-input" autocomplete="off" autofocus/>
      <button class="button">Send</button>
    </form>

In an useEffect hook, given below is my logic for socket.io integration Note that socket.io is already added as an npm package and imported into the document

  useEffect(() => {

    if (typeof (window) != undefined) {

      //Socket.io Custom Widget logic     
     
      const socket = io("https://website-demo.rasa.com/");


      socket.on('connect', function () {
        console.log("Connected to Socket.io server");
      });

      socket.on('connect_error', (error) => {
        // Write any connection errors to the console 
        console.error(error);
      });

      const messages = document.getElementById('messages')
      const form = document.getElementById('form')
      const messageInput = document.getElementById('message-input');


      form.addEventListener('submit', function (e) {
        e.preventDefault();
        const msg = messageInput.value;
        if (msg) {
          socket.emit('user_uttered', {
            "message": msg,
             
          });
          
          messageInput.value = '';
     
        appendMessage(msg, "sent");
      }
      });
      socket.on('bot_uttered', function (response) {
        console.log("Bot uttered:", response);
         if (response.text) {
             appendMessage(response.text, "received");
         }
         if (response.attachment) {
             appendImage(response.attachment.payload.src, "received");
         }
         if (response.quick_replies) {
             appendQuickReplies(response.quick_replies);
         }
     });
    

      function appendMessage(msg, type) {
        const item = document.createElement('div');
        item.textContent = msg;
        item.classList.add("message");
        item.classList.add(`message_${type}`);
        messages.appendChild(item);
        scrollToBottom();
      }
    }


  }, [])

Unfortunately, I do not get the bot_uttered messages listener to fire off upon successful submission of the form. Also I added the following to the credentials.yml file on the RasaX instance, but it still does not work

#credentials.yml

socketio:
  user_message_evt: user_uttered
  bot_message_evt: bot_uttered
  session_persistence: false
1 Like

Hello and Welcome @Sounav201 to the forum!

Can you please share the rasa version? are you using rasa open-source or rasa x?

Are you aware of the other integrations like rasa/botfront?

I was also facing the same issue. I am using rasa open-source version 2.8.13 and rasa x version 1.0.0 Yes I did try rasa/botfront, but I felt it had some limitations and wanted to build out my own frontend for my bot.

@snpranav what error message you getting while integrating?

@snpranav no worries, you can build you own but you can even customise the rasa botfront as per your use case, currently botfront covers all the features.

I followed the same tutorial - RASA - Socket.IO integration - DEV Community I configured by RasaX credentials.yml file with

socketio:
  user_message_evt: user_uttered
  bot_message_evt: bot_uttered
  session_persistence: false

I am able to establish the socketio connection, just like @Sounav201, I am not getting the listner to fire off upon successful submission of the form.

I added the same following lines in my code, but there are no console.log() statement is not getting triggered. since the bot_uttered listner is not getting triggered.

socket.on('bot_uttered', function (response) {
        console.log("Bot uttered:", response);
         if (response.text) {
             appendMessage(response.text, "received");
         }
         if (response.attachment) {
             appendImage(response.attachment.payload.src, "received");
         }
         if (response.quick_replies) {
             appendQuickReplies(response.quick_replies);
         }
     });

@snpranav can you share the error and which command you are using to run rasa? and please share the rasa version. Please reply these questions in one post please.

@nik202 I’m not getting an error from rasa, I’m not getting any response when I listen for bot_uttered using socket.io as shown in the code snippet above.
I don’t know if this is an error in the way I have written the code to communicate with the WebSocket or if it is an error in the way I have configured rasax.

Rasa Open Source Version - 2.8.13
Rasa X version - 1.0.0

I used the docker-compose installation method to deploy rasax with rasa.

@snpranav are you able to see the bot widget on your website or local machine ? can you share any screenshot for the same? confirm me are you able to talk with bot whilst using rasa x GUI?

I configured the same websocket of my rasax instance with botfront’s website chat widget and is working fine. Here’s a screenshot of it:

image

Yes I am able to talk to the bot whilst using the rasax GUI as well.

@snpranav That’s look great, can you tell me what IP address:port you have mentioned in index.html for the botfront and for the RASA - Socket.IO integration - DEV Community ? I hope you aware on this line at the end of the blog: " The web chat we created does not support all Rasa functionality and it could be improved in many ways. If you want to add Rasa chat to your webpage, consider rasa-webchat which provides more features."

@snpranav If you have DNS and HTTPs secure then there is no issue, I hope you aware the rasa x run on server IP with port 80 ?

For privacy reasons I do not want to share the websocket URL. However, I even tried this websocket - https://website-demo.rasa.com/

It works correctly with botfront, but fails to work using the code snippets from above.

@snpranav no worries, I totally understand. can you share credentials.yml file of rasa x server?

Do you mean the credentials.yml file on my rasax instance?

@snpranav yes correct where you mentioned socket.io channel? @snpranav Pranav you not answer on my above question, as you able to see the widget which petr7555 mention in the blog?

On my rasax instance

# credentials.yml file
rasa:
  url: ${RASA_X_HOST}/api

socketio:
  user_message_evt: user_uttered
  bot_message_evt: bot_uttered
  session_persistence: true

Sorry I don’t get what you mean by this question?

@snpranav That also looks perfect for me!

I mean the chatbot screen which is shown in the blog, are you able to run or see?

@snpranav hope you mentioned properly:

 const socket = io("https://<hostname>:80");

http://<host>:<port>

Please check this link : Your Own Website

Yes I am able to run it and see it

I mentioned it like this, but it still doesn’t trigger bot_uttered on the socket

@snpranav it should be https if you have secure domain just update and run. The issue is you not able to connect the rasa server with the snippet code.

@snpranav If you are using some welcome message using custom action code then, I hope you aware that we need an action_server image on server using docker-compose.override.yml

@snpranav OK, just one trick we can do to test this, run the ngrok on your server with the port 80 i.e ngrok http 80 or ./ngrok http 80 copy the https generate link and paste in const socket = io( ngrok link ) and try run.

@snpranav even I will suggest to run this on the local machine using rasa open source for the more clarity.

@snpranav I guess you don’t have the website, right?

Note: The above one to test the code is working or connecting or not ?