Skip to content

Unexpected automatic JSON parsing of WebSocket text frames in .ws() handler #1911

Description

@zohayrslileh

What version of Elysia is running?

elysia@1.4.28

What platform is your computer?

Darwin 25.5.0 arm64 arm

What environment are you using

Bun 1.3.14

Are you using dynamic mode?

No

What steps can reproduce the bug?

When sending a WebSocket text frame that contains JSON-like content, Elysia parses the payload before passing it to the .ws() message handler. This means the handler receives an array/object instead of the original text string.

This is a problem when using custom serialize/deserialize tools, because the raw text payload is expected to reach the server unchanged.

Steps to Reproduce

  1. Create a file with the following code:
import { Elysia } from "elysia"

new Elysia()

    .ws("/ws", {

        message(_ws, message) {

            console.log(message, typeof message)
        }
    })

    .listen(3000)

const ws = new WebSocket("ws://localhost:3000/ws")

ws.onopen = function () {

    ws.send('ping')

    ws.send('["ping"]')
}
  1. Run the file.

  2. Check the server output.

What is the expected behavior?

Both sent values are text frames, so the message handler should receive both values as strings:

ping string
["ping"] string

What do you see instead?

The second message is automatically parsed as JSON before reaching the handler:

ping string
[ "ping" ] object

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions