JSPM

websocket-polyfill

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3539
  • Score
    100M100P100Q123213F

WebSocket class from Browser to NodeJS

Package Exports

  • websocket-polyfill
  • websocket-polyfill/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (websocket-polyfill) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

WebSocket-Polyfill

Deprecated

This package had been developed very long time ago.

It had utilized the websocket library, but the polyfill could be just done by only one line with ws library. In such reason, websocket-polyfill no more keeps long line of codes, but just have only one line like below.

I think instead of importing this websocket-polyfill library, it would better to write the one line code by yourself.

import import2 from "import2"; // JUST TO AVOID TRANSPILING
import { is_node } from "tstl"; // YOU CAN REPLACE IT LIKE BELOW

// let is_node_: boolean | null = null;
// const is_node = (): boolean =>
//   (is_node_ ??=
//     typeof global === "object" &&
//     typeof global.process === "object" &&
//     typeof global.process.versions === "object" &&
//     typeof global.process.versions.node !== "undefined");

if (is_node()) (global as any).WebSocket ??= import2("ws");

Outline

WebSocket class for NodeJS.

Installation

NPM Module

npm install --save websocket-polyfill

Usage

import "websocket-polyfill";

const main = (): void => {
  const ws: WebSocket = new WebSocket("ws://127.0.0.1:38000/main");
  ws.onmessage = (msg: MessageEvent) => {
    console.log("Data from ws-server", msg.data);
  };
}
main();