https://github.com/breqdev/workersocket
Run a WebSocket inside of a Web Worker.
https://github.com/breqdev/workersocket
browser javascript websocket webworker
Last synced: 8 months ago
JSON representation
Run a WebSocket inside of a Web Worker.
- Host: GitHub
- URL: https://github.com/breqdev/workersocket
- Owner: breqdev
- License: mit
- Created: 2022-01-07T16:27:09.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-03T19:23:22.000Z (over 3 years ago)
- Last Synced: 2025-08-09T19:31:20.301Z (9 months ago)
- Topics: browser, javascript, websocket, webworker
- Language: TypeScript
- Homepage: https://breq.dev/projects/workersocket/
- Size: 114 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# workersocket
A WebSocket that runs in a Web Worker.
Published as an ES module only.
## Node.js
Web Workers are not available in Node.js. As a fallback, this module will return a `ws` object. This is intended to support both dual browser/node libraries and browser libraries that run unit tests in Node.
## Deps
- [`ws`](https://www.npmjs.com/package/ws) to support use with Node. Adds nothing to a browser bundle.
## Example
This library strives to be as similar to the WebSocket API as possible.
```js
import { WorkerSocket } from "workersocket";
const socket = new WorkerSocket("ws://echo.websocket.events");
socket.onmessage = (event) => {
console.log(event.data);
socket.close();
};
socket.send("Hello, world!");
```