Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Types for WebSocketPair #84

Closed
ssttevee opened this issue Apr 2, 2021 · 6 comments · Fixed by #112
Closed

Types for WebSocketPair #84

ssttevee opened this issue Apr 2, 2021 · 6 comments · Fixed by #112

Comments

@ssttevee
Copy link

ssttevee commented Apr 2, 2021

Any plans for adding types related to websockets as seen in https://github.com/cloudflare/workers-chat-demo?

There doesn't seem to be any documentation on the the cloudflare docs so even writing it myself would be difficult. 😢

@sepbot
Copy link

sepbot commented Apr 4, 2021

export {}

declare global {
  interface WebSocket {
    accept(): void;
  }

  class WebSocketPair {
    0: WebSocket;
    1: WebSocket;
  }

  interface ResponseInit {
    webSocket?: WebSocket;
  }
}

@stof
Copy link

stof commented Apr 14, 2021

the doc is now available at https://developers.cloudflare.com/workers/runtime-apis/websockets

@hronro
Copy link

hronro commented Jul 5, 2021

Any updates?
Why those types are not included in this library?

@helloimalastair
Copy link

Any updates?
Why those types are not included in this library?

Try adding a pull request, it might make it faster for them to make changes if all they have to do is review your code.

@OwenDelahoy
Copy link

OwenDelahoy commented Sep 1, 2021

export { }

declare global {
  interface CloudflareWebsocket {
    accept(): unknown;
    addEventListener(event: 'close', callbackFunction: (code?: number, reason?: string) => unknown): unknown;
    addEventListener(event: 'error', callbackFunction: (e: unknown) => unknown): unknown;
    addEventListener(event: 'message', callbackFunction: (event: { data: any }) => unknown): unknown;
    
    /**
     * @param code https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
     * @param reason
     */
    close(code?: number, reason?: string): unknown;
    send(message: string|Uint8Array): unknown;
  }

  class WebSocketPair {
    0: CloudflareWebsocket; // Client
    1: CloudflareWebsocket; // Server
  }

  interface ResponseInit {
    webSocket?: CloudflareWebsocket;
  }
}

@aboodman
Copy link

aboodman commented Sep 18, 2021

Warning to anyone else using these types:

Using sepbot's suggestion (#84 (comment)) has the effect of adding accept() to any pre-existing WebSocket definition. In most cases, this will be the standard DOM WebSocket in your code.

This would be fine if Cloudflare Workers actually implemented the complete WebSocket spec, but it doesn't. For example, the readyState property from the spec is not currently supported.

I suggest using instead something like OwenDelahoy's suggestion (#84 (comment)) will narrowly defines only the features actually supported by CF workers.

@mrbbot mrbbot linked a pull request Sep 24, 2021 that will close this issue
threepointone added a commit that referenced this issue Sep 30, 2021
Auto-Generated Types

Types are now automatically generated with releases by parsing the runtime's source code, and merging in overrides/docs defined in this repository (for generics, overloads, comments, etc).

webworker no longer needs to (and shouldn't) be included in tsconfig lib.

The final merged AST that's used to render the TypeScript types, workers.json, is also included in the repository. This could be used to generate bindings for other languages that compile to WebAssembly. Rust output is coming soon.

Closes: #55, #75, #76, #81, #84, #96, #97, #100, #101, #102, #105, #106, #107, #108
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants