Skip to content

πŸ› Bug Report β€” WebSocket API client connection throws internal errorΒ #5452

@AustinZhu

Description

@AustinZhu

Description

When using WebSocket API in the Workers handler to initiate a connection as client, it throws an internal error, disrupting the Upgrade connection. The remote server itself is reachable and can accept the connection successfully in other runtimes (like Bun and Node.js) and tools like Postman.

The method in the WebSocket API documentation also does not work in this case.

Error Detail

✘ [ERROR] WebSocket error: ErrorEvent {

    filename: '',
    message: 'Uncaught Error: internal error; reference = lu5vo78l8a10giumjpb6k6j6',
    lineno: 0,
    colno: 0,
    error: [Error: internal error; reference = lu5vo78l8a10giumjpb6k6j6] {
      [stack]: [Getter/Setter],
      [message]: 'internal error; reference = lu5vo78l8a10giumjpb6k6j6',
      remote: true
    },
    type: 'error',
    eventPhase: 2,
    composed: false,
    bubbles: false,
    cancelable: false,
    defaultPrevented: false,
    returnValue: true,
    currentTarget: WebSocket {
      readyState: 3,
      url: 'wss://openmd.shinnytech.com/t/md/front/mobile',
      protocol: '',
      extensions: ''
    },
    target: WebSocket {
      readyState: 3,
      url: 'wss://openmd.shinnytech.com/t/md/front/mobile',
      protocol: '',
      extensions: ''
    },
    srcElement: WebSocket {
      readyState: 3,
      url: 'wss://openmd.shinnytech.com/t/md/front/mobile',
      protocol: '',
      extensions: ''
    },
    timeStamp: 0,
    isTrusted: true,
    cancelBubble: false,
    NONE: 0,
    CAPTURING_PHASE: 1,
    AT_TARGET: 2,
    BUBBLING_PHASE: 3
  }


WebSocket connection closed

Sample Code

This will output WebSocket connection opened in Bun but will throw the above error in Workers runtime.

export default {
  async fetch(request): Promise<Response> {
    const ws = new WebSocket('wss://openmd.shinnytech.com/t/md/front/mobile');

    ws.addEventListener('open', () => {
      console.log('WebSocket connection opened');
    });

    ws.addEventListener('close', () => {
      console.log('WebSocket connection closed');
    });

    ws.addEventListener('error', (event) => {
      console.error('WebSocket error:', event);
    });

    ws.addEventListener('message', (event) => {
      console.log('Received message:', event.data);
    });

    await new Promise((resolve) => setTimeout(resolve, 5000));

    return new Response('Something', {
      headers: {
        'content-type': 'text/html;charset=UTF-8',
      },
    });
  },
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions