Skip to content

WebSocket ​

Eden Treaty supports WebSocket using subscribe method.

typescript
import { Elysia, t } from 'elysia'
import { treaty } from '@elysiajs/eden'

const app = new Elysia()
    .ws('/chat', {
        body: t.String(),
        response: t.String(),
        message(ws, message) {
            ws.send(message)
        }
    })
    .listen(3000)

const app = treaty<typeof app>('localhost:3000')

const chat = app.chat.subscribe()

chat.subscribe((message) => {
    console.log('got', message)
})

chat.send('hello from client')
import { Elysia, t } from 'elysia'
import { treaty } from '@elysiajs/eden'

const app = new Elysia()
    .ws('/chat', {
        body: t.String(),
        response: t.String(),
        message(ws, message) {
            ws.send(message)
        }
    })
    .listen(3000)

const app = treaty<typeof app>('localhost:3000')

const chat = app.chat.subscribe()

chat.subscribe((message) => {
    console.log('got', message)
})

chat.send('hello from client')

.subscribe accepts the same parameter as get and head.

Response ​

Eden.subscribe returns EdenWS which extends the WebSocket results in identical syntax.

If more control is need, EdenWebSocket.raw can be accessed to interact with the native WebSocket API.