The high-level web standard WebSocket() API and the low-level ECMA-419 embedded standard WebSocket Client APIs are both available to Embedded JavaScript developers using the Moddable SDK. In addition, the Web platform's experimental WebSocketStream() is available. Both WebSocket() and WebSocketStream() are implemented using WebSocket Client.
- Connect to Server
- Connect Securely to Server
- Close Connection
- Send Message
- Receive Message
- Connection Information
- Connect to Server
- Connect Securely to Server
- Close Connection
- Send Message
- Receive Message
- Control Messages
If WebSocket() works for your project, you should use it. You can always start with WebSocket() and switch to WebSocket Client later. With WebSocket() you'll usually write less code. Because WebSocket() is a well-known web standard, many developers are already familiar with it.
WebSocketStream() is built on streams, part of the modern Web platform. This simplifies composing data flows. WebSocketStream() also addresses a fundamental problem with WebSocket(), the lack of support for backpressure, which can lead to memory and buffering problems. For low throughput connections, the lack of backpressure support is not usually an issue.
Situations where WebSocket() and WebSocketStream() might not be the best choice include when resources like RAM and ROM are very constrained, when you have large messages, and when you need precise control over the buffering of messages.
Using WebSocket Client requires more code and a deeper understanding of the WebSocket protocol. But, it offers real advantages:
- Lower runtime overhead
- Send and receive messages of unlimited size
- Send and receive control messages
- Smaller firmware size
Include the WebSocket() manifest in your project's manifest.json:
$(MODDABLE)/examples/io/tcp/websocket/manifest_websocket.json
Then, import the module in your JavaScript source code:
import WebSocket from "WebSocket";To use the WebSocket Client instead, include its manifest. Including the manifest initializes the host provider device.network.ws:
$(MODDABLE)/examples/io/tcp/websocketclient/manifest_websocketclient.json
For secure connections over WSS, include the secure WebSocket Client manifest instead. It initializes the host provider device.network.wss:
$(MODDABLE)/examples/io/tcp/websocketsclient/manifest_wssclient.json
To use WebSocketStream(), include its manifest in your project's manifest.json.
$(MODDABLE)/modules/web/streams/websocket/manifest.json
Then, import the module in your JavaScript source code:
import WebSocketStream from "web/websocketstream";If your project uses the WebSocket global, mcpack automatically includes the manifest and initializes the WebSocket global. If your project uses WebSocket in an import statement, its manifest is automatically included.
If your project accesses the WebSocket Client through device.network.ws or device.network.wss, mcpack includes the necessary manifests automatically.
If your project uses the WebSocketStream global, mcpack automatically includes the manifest and initializes the WebSocketStream global. If your project uses web/websocketstream in an import statement, its manifest is automatically included.
-
Documentation
- WebSocket() on MDN
- WebSocketStream() on MDN
- WebSocketStream Intro on "Chrome for Developers"
-
Standards
- WebSocket protocol RFC
- WebSocket in the WHATWG WebSockets Standard
- WebSocket Client in ECMA-419
-
Examples
- WebSocket() example in Moddable SDK
- WebSocket Client example in Moddable SDK
- WebSocketStream() example in Moddable SDK
- Touch over WebSocketStream() example in Moddable SDK
-
Implementations
- WebSocket() in Moddable SDK
- WebSocket Client in Moddable SDK
- WebSocketStream() in Moddable SDK
-
TypeScript Declarations
- WebSocket() in Moddable SDK
- WebSocket Client in Moddable SDK
- WebSocketStream() in Moddable SDK