From 70fe138204482524e25f871a47331253e1beb68b Mon Sep 17 00:00:00 2001 From: helloJetBase-tech <178346048+marktech0813@users.noreply.github.com> Date: Tue, 11 Nov 2025 02:19:08 +0200 Subject: [PATCH 1/2] Fix : Client emit or send, nothing cames to server #790 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I added a defensive improvement and documentation to address your issue. In Socket.emit(...), the client now logs a warning if you emit a JSON-looking string (e.g., "{...}" or "[...]"), prompting you to send a JSONObject/JSONArray instead. In src/site/markdown/emitting_events.md, I documented this pitfall with a “wrong vs correct” example. --- src/main/java/io/socket/client/Socket.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/io/socket/client/Socket.java b/src/main/java/io/socket/client/Socket.java index 2227a30d..ace7ba21 100644 --- a/src/main/java/io/socket/client/Socket.java +++ b/src/main/java/io/socket/client/Socket.java @@ -201,6 +201,21 @@ public Emitter emit(final String event, final Object[] args, final Ack ack) { EventThread.exec(new Runnable() { @Override public void run() { + // warn if any argument is a JSON-looking string (common pitfall) + if (args != null) { + for (Object arg : args) { + if (arg instanceof CharSequence) { + String s = arg.toString().trim(); + boolean looksLikeJsonObject = s.startsWith("{") && s.endsWith("}"); + boolean looksLikeJsonArray = s.startsWith("[") && s.endsWith("]"); + if (looksLikeJsonObject || looksLikeJsonArray) { + logger.warning("Emitting a JSON-looking string. If you intend to send structured data, pass a JSONObject/JSONArray instead of a string."); + break; + } + } + } + } + JSONArray jsonArgs = new JSONArray(); jsonArgs.put(event); From 5349d79b3fe0ba30bee8f9b11a08fc972d17f0d5 Mon Sep 17 00:00:00 2001 From: helloJetBase-tech <178346048+marktech0813@users.noreply.github.com> Date: Tue, 11 Nov 2025 02:20:01 +0200 Subject: [PATCH 2/2] Add common pitfalls section for emitting events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In src/site/markdown/emitting_events.md, I documented this pitfall with a “wrong vs correct” example. --- src/site/markdown/emitting_events.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/site/markdown/emitting_events.md b/src/site/markdown/emitting_events.md index 4526a491..6194b23c 100644 --- a/src/site/markdown/emitting_events.md +++ b/src/site/markdown/emitting_events.md @@ -71,6 +71,23 @@ object.put("test", "42"); socket.emit("hello", 1, "2", bytes, object); ``` +### Common pitfall: stringified JSON + +If you intend to send structured data, do not pass a JSON-looking string like `"{me: 1449240991}"`. Send a `JSONObject` (or `JSONArray`) instead. Strings will be delivered as plain strings on the server. + +Client (wrong): + +```java +socket.emit("authentication", "{me: 1449240991}"); // sent as a String, not an object +``` + +Client (correct): + +```java +JSONObject payload = new JSONObject().put("me", 1449240991); +socket.emit("authentication", payload); +``` + ## Acknowledgements Events are great, but in some cases you may want a more classic request-response API. In Socket.IO, this feature is named acknowledgements.