Registry / messaging / as-websocket

as-websocket

JSON →
library1.1.2jsnpmunverified

WebSocket bindings for AssemblyScript, enabling real-time communication in WebAssembly modules. Current stable version 1.1.2 provides a simple API to create WebSocket servers and clients directly in AssemblyScript, with automatic integration via wasmImports and wasmExports. Released under MIT, it supports both Node.js and browser environments through a UMD bundle. Key differentiator: native WebSocket support in AssemblyScript without JavaScript glue code, leveraging the official ws package on Node. Development appears slow with few updates.

npm install as-websocket
INSTALL
IMPORT
SIG · AS-WEBSOCKET
A
as-websocket
messagingjavascriptv1.1.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WebSocket
import { WebSocket } from 'as-ws'
import WebSocket from 'as-websocket'
The class is named 'WebSocket' and exported from 'as-ws', not the package name 'as-websocket'.
default export
const asWebSocket = require('as-websocket')
In Node.js, the require returns a constructor function that creates the bridge object.
browser global
<script src='https://unpkg.com/as-websocket@latest/browser.js'></script> // then use 'asWebSocket' global
import { asWebSocket } from 'as-websocket'
Browser build exposes a global variable 'asWebSocket', not an ES module.

Shows how to create a WebSocket bridge in Node.js using as-websocket and load an AssemblyScript module that uses the WebSocket class.

// Node.js server setup const asWebSocket = require('as-websocket'); const asBindings = new asWebSocket(); const imports = { ...asBindings.wasmImports }; // Your AssemblyScript module (compiled with --exportTable and --exportRuntime) const wasmModule = loader.instantaniateSync(wasmBytes, imports); asBindings.wasmExports = wasmModule.exports; // AssemblyScript source (file.wat or .ts): // import { WebSocket } from 'as-ws'; // const socket = new WebSocket('ws://localhost:3000'); // socket.on('message', (data) => { // console.log('Message: ' + data); // }); // socket.send('Hello From AssemblyScript!');
Debug
Known issues
gotchaThe import path for the AssemblyScript library is 'as-ws', not 'as-websocket'.
fix
Use 'import { WebSocket } from "as-ws"' in AssemblyScript source.
affects: >=1.0.0
breakingRequires AssemblyScript's --exportTable and --exportRuntime flags when compiling WASM modules.
fix
Add these flags to your asc command line: 'asc file.ts --exportTable --exportRuntime'
affects: >=1.0.0
gotchaBrowser UMD bundle is loaded via script tag and exposes global 'asWebSocket', not an ES module.
fix
Use the script tag from unpkg and access the global asWebSocket constructor.
affects: >=0.0.1
deprecatedThe constructor asWebSocket() returns an object, not a WebSocket instance. The WebSocket class is only available in AssemblyScript.
fix
Understand that asWebSocket is a bridge factory; actual WebSocket usage occurs inside AssemblyScript code.
affects: >=1.0.0
Errors
Common errors & fixes
Module not found: 'as-ws'
Importing 'as-websocket' in AssemblyScript instead of 'as-ws'
fix
Change import statement to 'from "as-ws"'
TypeError: asWebSocket is not a constructor
Using ES module import instead of require in Node.js
fix
Use 'const asWebSocket = require("as-websocket")'
Unexpected token 'export' (browser)
Attempting to import ES module syntax in browser without a bundler
fix
Use the UMD script tag and access the global 'asWebSocket' variable
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies
assemblyscriptrequiredPeer dependency required for compilation and runtime
wsoptionalUsed internally for WebSocket implementation in Node.js
Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources
as-websocket — npm install as-websocket · libregistry