i am getting internal server error while using the message action api in pubnub

83 views Asked by At

I am using Pubnub for a chat feature I am building. I am trying to use the message actions API to add an action value to an already published message as described in the documentation found here message action api. But whenever I run my code I get the error below:

NOTE: I removed certain components from the error response because it contained sensitive data!

The error response:

{
  "error": true,
  "operation": "PNAddActionOperation",
  "statusCode": 500,
  "errorData": {
    "original": null,
    "response": {
      "req": {
        "method": "POST",
        "url": "https://ps18.pndsn.com/v1/message-actions/my_sub_key/channel/my_channel_name/message/17053072012353411?uuid=my_user_uuid&pnsdk=PubNub-JS-Web%2F7.4.5&l_hist=791&l_pres=1733",
        "data": {
          "type": "isRead",
          "value": true
        },
        "headers": {
          "content-type": "application/json"
        }
      },
      "xhr": {},
      "text": "<html>\r\n<head><title>500 Internal Server Error</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>500 Internal Server Error</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n",
      "statusText": "Internal Server Error",
      "statusCode": 500,
      "status": 500,
      "statusType": 5,
      "info": false,
      "ok": false,
      "redirect": false,
      "clientError": false,
      "serverError": true,
      "error": {
        "status": 500,
        "method": "POST",
        "url": "https://ps18.pndsn.com/v1/message-actions/my_sub_key/channel/my_channel_name/message/17053072012353411?uuid=my_user_uuid&pnsdk=PubNub-JS-Web%2F7.4.5&l_hist=791&l_pres=1733"
      },
      "created": false,
      "accepted": false,
      "noContent": false,
      "badRequest": false,
      "unauthorized": false,
      "notAcceptable": false,
      "forbidden": false,
      "notFound": false,
      "unprocessableEntity": false,
      "headers": {
        "content-length": "588",
        "content-type": "text/html"
      },
      "header": {
        "content-length": "588",
        "content-type": "text/html"
      },
      "type": "text/html",
      "links": {},
      "body": null
    },
    "status": 500
  },
  "category": "PNUnknownCategory"
}

My JS Code:

let pubnub;

const pubnub_publish_key = 'demo'
const pubnub_subscribe_key = 'demo'

const user_id = 'test_user_id'

const my_channel_name = 'test_channel'

const setupPubNub = () => {
  // Initialize PubNub with the publish and subscribe keys and the current user's ID
  pubnub = new PubNub({
    publishKey: pubnub_publish_key,
    subscribeKey: pubnub_subscribe_key,
    userId: user_id,
    reconnectPolicy: 'linear'
  });

  await pubnub.subscribe({
    channels: [my_channel_name],
    presence: true,
  });
  
  fetchMessages(my_channel_name)

  const listener = {
    status: (statusEvent) => {
      if (statusEvent.category === "PNConnectedCategory") {
        console.log("Connected")
      }
    },
    message: (messageEvent) => {
      console.log(messageEvent)
    },
    presence: function(presenceEvent) {
      console.log(presenceEvent)
    }
  };

  pubnub.addListener(listener);
};

window.onload = setupPubNub;
window.onbeforeunload = (event) => {
  pubnub.unsubscribeAll();
}


// Asynchronous function to fetch messages from a channel
const fetchMessages = async(channelName) => {
  try {
    // Fetch messages from the channel
    await pubnub.fetchMessages({
      channels: [channelName],
      count: 100,
      stringifiedTimeToken: true,
    }, (status, response) => {
      if (status.error) {
        throw new Error(JSON.stringify(status));
      } else {
        // Encode the channel name (becaues it contains a colon)
        const encodedChannelName = encodeURIComponent(channelName)
        if (response) {
          // Get the messages from the response
          const messages = response.channels[encodedChannelName]

          // If there are messages
          if (messages) {
            // Wrap the loop in an async function
            (async() => {
              // For each message
              for (let i = 0; i < messages.length; i++) {
                let {
                  uuid,
                  timetoken,
                  message: {
                    text: messageText
                  }
                } = messages[i];

                //other code goes here to insert the message text and time to ket to the ui. Removed it becasue it doesn't matter for the error i am getting

                // If this is the last message
                if (i == messages.length - 1) {
                  let lastMessageTimetoken = messages[i] ? .timetoken

                  //logs the last message data properly
                  console.log(channelName, lastMessageTimetoken)

                  const isRead = messages[i].message ? .isRead ? true : await updateIsRead(channelName, lastMessageTimetoken);

                  console.log(isRead)
                }
              }
            })();
          }
        }
      }
    });
  } catch (error) {
    // Log any errors that occurred
    console.error(`Error in fetchMessages: ${error.message}`);
  }
}

//this is where i get the error
const updateIsRead = async(channelName, timetoken) => {
  if (typeof channelName !== 'string' || typeof timetoken !== 'string') {
    console.error('Invalid parameters');
    return;
  }
  await pubnub.addMessageAction({
      channel: channelName,
      messageTimetoken: timetoken,
      action: {
        type: 'isRead',
        value: true,
      },
    },
    function(status, response) {
      if (status.error) {
        return status
      } else {
        return response
      }
    }
  );
}

1

There are 1 answers

2
seriously On

The error is caused in this snippet:

await pubnub.addMessageAction({
    channel: channelName,
    messageTimetoken: timetoken,
    action: {
      type: 'isRead',
      value: true,
    },
  },
  function(status, response) {
    if (status.error) {
      return status
    } else {
      return response
    }
  }
);

The problem is that all the values inside the action object should be a string. I used a boolean and that's what caused the issue. The documentation specifies that I should use a string value I overlooked. However, an internal server error response for this error is misinformative.

This fixes the issue:

await pubnub.addMessageAction({
        channel: channelName,
        messageTimetoken: timetoken,
        action: {
          type: 'isRead',
          value: 'true',
        },
      },
      function(status, response) {
        if (status.error) {
          return status
        } else {
          return response
        }
      }
    );