ESP 32 client is not connecting to node socketio server

I am quite new to arduino. I want to develop a esp-32 client which can connect to my custom node socketio server. I have tried may libraries and many approaches, but none of them works. Everytime my esp-32 is just logging Disconnecting. Can anyone help me how can I connect my esp-32 to socketio server? I am attaching my esp code below:

#include <Arduino.h>
#include <WiFi.h>
#include <SocketIoClient.h>

const char *ssid = "Nord N100";                         // Enter SSID
const char *password = "123456789";                     // Enter Password
const char *websockets_server_host = "192.168.181.98";  // Enter server adress
const uint16_t websockets_server_port = 8080;           // Enter server port

SocketIoClient webSocket;

void event(const char *payload, size_t length){
    Serial.printf("got message: %s\n", payload);
}

void setup() {
  Serial.begin(9600);
  Serial.setDebugOutput(true);
  
  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  // Connect to wifi
  WiFi.begin(ssid, password);

  // Wait some time to connect to wifi
  for (int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++) {
    Serial.print(".");
    delay(1000);
  }

  // Check if connected to wifi
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("No Wifi!");

    WiFi.disconnect();
    WiFi.reconnect();
    delay(500);
  }

  webSocket.on("event", event);

  Serial.println("Connected to Wifi, Connecting to server.");
  // try to connect to Websockets server
  webSocket.begin("192.168.181.98", websockets_server_port, "/socket.io/?transport=websocket");
}

void loop() {
  webSocket.loop();
  delay(500);
}

I don't see anywhere in your sketch that logs "Disconnecting" so where are you seeing those messages? What are you seeing in Serial Monitor?

Have you tried the ESP32BasicExample that came with the SocketIoClient library?

I also dont know where this Disconnecting is coming from.

Yes, I have also tried the basic example, but it doesn't connect either.

Hey, I am running into the exact same problem, have you been able to find a solution?

yes. first try to connect it via the example, then write your own based on the example

hi @saif_hossain did you get it working?
can you please share your code? how you got it to work?

Can you share your server and client code? i have the same problem

what is your client/server attempting to do? e.g. transfer data from lient to server using a TCP protocol? what protocol?
give use more details of the application?

yeah, I solved it.
first tested the example code if it is connecting or not. It works via example code. then modified the example code.

1 Like

it is just to establish connections between different mobiles to transfer a mass amount of data.
it used websocket protocol.

It is used to make a visual representation of data in real time from multiple sensors.

#include <WebSocketsClient.h>
#include <WiFi.h>

const char* ssid = "xxxxx";
const char* password = "xxxxxxxx";
const char* serverIP = "xxx.xxx.x.213";
const int serverPort = 3999; // The port you used for the server

WebSocketsClient webSocket;

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

webSocket.begin(serverIP, serverPort);
webSocket.onEvent(webSocketEvent);
}

void loop() {
webSocket.loop();
}

void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_CONNECTED:
Serial.println("Connected to server");
break;
case WStype_TEXT:
Serial.print("Message from server: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
break;
case WStype_DISCONNECTED:
Serial.println("Disconnected from server");
break;
}
}
With this code, I see the output of back to back a connection and no connection on the serial monitor, but nothing goes to the 213 ip address.

Hi, I have the same problem as you, I used the code in the example, it doesn't work, and I wrote my own based on the principle from the example, it doesn't work either. Could you help me? I have a web server with sockets on Flask

Flask:

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
socket = SocketIO(app)

@socket.on("connect")
def handle_connect():
    print(" Client connected ")

@socket.on("message")
def handle_message(data):
    print(f"Data: {data}" )
    socket.emit("message", data)

socket.run(app, debug=True, host="ip", port=5000)

ESP32:

#include <WiFi.h>
#include <SocketIoClient.h>

const char* ssid = "MIG 5";
const char* password = "00050878";

char host[] = "ip";
int port = 5000;

SocketIoClient webSocket;
WiFiClient client;

void onConnect(const char * payload, size_t length) {
  Serial.println("Socket.IO Connected!");
}

void onMessage(const char * payload, size_t length) {
  webSocket.emit("message", "message");
}

void setup() {
  Serial.begin(9600);
  delay(10);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("WiFi connected");  

  webSocket.on("connect", onConnect);
  webSocket.on("message", onMessage);

  webSocket.begin(host, port);
}

void loop() {
  webSocket.loop();
  webSocket.emit("message", "123123");
  delay(1000);
}

@saif_hossain where cani find the example code