[PR #5985] fix(common): parse JSON message body before emitting via Socket.IO #5448

Open
opened 2026-03-17 02:53:24 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hoppscotch/hoppscotch/pull/5985
Author: @mahmoodhamdi
Created: 3/13/2026
Status: 🔄 Open

Base: mainHead: fix/socketio-json-message-parsing


📝 Commits (2)

  • 2563f2d fix(common): parse JSON message body before emitting via Socket.IO
  • 3b5f71c fix(common): restrict JSON parsing to objects/arrays and sync sent-event log

📊 Changes

1 file changed (+16 additions, -2 deletions)

View changed files

📝 packages/hoppscotch-common/src/helpers/realtime/SIOConnection.ts (+16 -2)

📄 Description

What does this PR do?

Fixes the Socket.IO client to parse JSON message bodies into actual objects before emitting them. Previously, JSON strings typed in the message body editor were emitted as-is, causing double-serialization on the wire.

Why?

I was testing a Socket.IO server and noticed the messages being sent had double-serialized JSON payloads. For example, sending {"key": "value"} through the Socket.IO tab would produce:

420["test-message","{\n  \"key\": \"value\"\n}"]

instead of the correct format:

42["test-message",{"key":"value"}]

This happens because socket.emit() receives a string and serializes it again. Real socket.io clients (like the npm package) accept objects directly, so emit(event, {key: value}) produces the correct wire format.

Changes

  • In SIOConnection.sendMessage(), the message body is now parsed via JSON.parse() before being passed to socket.emit()
  • If the message is not valid JSON (plain text), it falls back to sending the raw string as before
  • This is a minimal, safe change — only the emit payload is affected

Testing

  • All existing tests pass (680 tests in hoppscotch-common)
  • TypeScript type checking passes
  • ESLint passes with no new warnings
  • Verified behavior:
    • Sending {"key": "value"} now correctly emits as a JSON object
    • Sending plain text like hello still works as a string
    • Sending arrays [1, 2, 3] correctly emits as an array
    • Sending numbers 42 correctly emits as a number

Closes #4570


Summary by cubic

Parse JSON message bodies before emitting via Socket.IO so objects and arrays are sent as native values instead of double-serialized strings. Only parse payloads starting with { or [ to avoid coercing primitives, and log the actual emitted payload in the sent-event history; fall back to the raw string for invalid JSON.

Written for commit 3b5f71cd0a. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hoppscotch/hoppscotch/pull/5985 **Author:** [@mahmoodhamdi](https://github.com/mahmoodhamdi) **Created:** 3/13/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/socketio-json-message-parsing` --- ### 📝 Commits (2) - [`2563f2d`](https://github.com/hoppscotch/hoppscotch/commit/2563f2db6cc9375a694359bbe23752be343fec0a) fix(common): parse JSON message body before emitting via Socket.IO - [`3b5f71c`](https://github.com/hoppscotch/hoppscotch/commit/3b5f71cd0a6aea786aed2e3ce97ae0b887b7a2a6) fix(common): restrict JSON parsing to objects/arrays and sync sent-event log ### 📊 Changes **1 file changed** (+16 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/hoppscotch-common/src/helpers/realtime/SIOConnection.ts` (+16 -2) </details> ### 📄 Description ## What does this PR do? Fixes the Socket.IO client to parse JSON message bodies into actual objects before emitting them. Previously, JSON strings typed in the message body editor were emitted as-is, causing double-serialization on the wire. ## Why? I was testing a Socket.IO server and noticed the messages being sent had double-serialized JSON payloads. For example, sending `{"key": "value"}` through the Socket.IO tab would produce: ``` 420["test-message","{\n \"key\": \"value\"\n}"] ``` instead of the correct format: ``` 42["test-message",{"key":"value"}] ``` This happens because `socket.emit()` receives a string and serializes it again. Real socket.io clients (like the npm package) accept objects directly, so `emit(event, {key: value})` produces the correct wire format. ## Changes - In `SIOConnection.sendMessage()`, the message body is now parsed via `JSON.parse()` before being passed to `socket.emit()` - If the message is not valid JSON (plain text), it falls back to sending the raw string as before - This is a minimal, safe change — only the emit payload is affected ## Testing - All existing tests pass (680 tests in hoppscotch-common) - TypeScript type checking passes - ESLint passes with no new warnings - Verified behavior: - Sending `{"key": "value"}` now correctly emits as a JSON object - Sending plain text like `hello` still works as a string - Sending arrays `[1, 2, 3]` correctly emits as an array - Sending numbers `42` correctly emits as a number ## Related Issues Closes #4570 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Parse JSON message bodies before emitting via Socket.IO so objects and arrays are sent as native values instead of double-serialized strings. Only parse payloads starting with { or [ to avoid coercing primitives, and log the actual emitted payload in the sent-event history; fall back to the raw string for invalid JSON. <sup>Written for commit 3b5f71cd0a6aea786aed2e3ce97ae0b887b7a2a6. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#5448
No description provided.