MQTT(pubsubclient) very slow to respond to message on ESP32

I have a new ESP32 board. No hardware connected, just the board plugged in to my usb cable.

I have set up WiFi and it connects without issue.

I have set up the pubsubclient and have my mqtt server on my home network. When the ESP32 connects, it publishes a message saying connected just fine.

But when I publish a message on my network, the ESP32 is very slow (and randomly slow) to respond that it received the message. Sometimes it reacts to the message in 2-3 seconds, sometimes it sits for 60-90 seconds before finally acknowledging the message arrived. Sometimes it seems the message doesn't get through at all and the board has to 'reconnect' to the mqtt server.

Here is my code.

#include <WiFi.h>
#include <PubSubClient.h>

const char* mqtt_server = "10.11.151.10"; //mqtt server
const char* ssid = "";
const char* password = "";

WiFiClient espClient;
PubSubClient client(espClient); //lib required for mqtt



void callback(char* topic, byte* payload, unsigned int length) {   //callback includes topic and payload ( from which (topic) the payload is comming)
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
  }
  Serial.println("");
  
}

void reconnect() {
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    if (client.connect("ESP32_clientID")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "Nodemcu connected to MQTT");
      // ... and resubscribe
      client.subscribe("inTopic");

    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void connectmqtt()
{
  client.connect("ESP32_clientID");  // ESP will connect to mqtt broker with clientID
  {
    Serial.println("connected to MQTT");

    client.subscribe("inTopic"); //topic=Demo
    client.publish("outTopic",  "connected to MQTT");

    if (!client.connected())
    {
      reconnect();
    }
  }
}

void setup()
{
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("connected");
  client.setServer(mqtt_server, 1883);//connecting to mqtt server
  client.setCallback(callback);
  //delay(5000);
  connectmqtt();
}


void loop()
{
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();
}

I have similar code I've used with a Wemos D1 Mini and I do not have any issues with delay. I publish a message on my server and they basically immediately respond to the published mqtt message. Is there something wrong with my code? Is maybe my board not working right? I must admit it seems like it's not as stable with a WiFi connection, but I can't say for sure why it feels that way.

How do I get the ESP32 to respond immediately to my mqtt messages?

I don't know what the issues is, but it's "fixed" now, so I'll share my experience. The ESP32 I was having trouble with was an Inland ESP32 I bought from Microcenter. I could never get it to work. I ended up swapping to an Adafruit feather ESP32. I didn't change the code at all, and it works perfect and responds immediately. Don't know if I just had a bad board, or if there is something wrong with the Inland boards with mqtt in general. I only had the one to try. But it works now on the Adafruit board. Maybe this will help someone else if they run into a similar issue with the same hardware.