https://github.com/compulim/messageport-websocket
Turns HTML MessagePort into HTML WebSocket
https://github.com/compulim/messageport-websocket
Last synced: about 1 year ago
JSON representation
Turns HTML MessagePort into HTML WebSocket
- Host: GitHub
- URL: https://github.com/compulim/messageport-websocket
- Owner: compulim
- Created: 2018-05-13T20:13:35.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-19T22:51:05.000Z (about 8 years ago)
- Last Synced: 2025-02-17T13:11:25.597Z (over 1 year ago)
- Language: JavaScript
- Size: 205 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# messageport-websocket
[](https://badge.fury.io/js/messageport-websocket) [](https://travis-ci.org/compulim/messageport-websocket)
This package will turn [MessagePort](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort) into [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
# Background
Instead of learning/using different API for different communication channels, we should unite them into a single interface pattern, either [MessagePort](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort) or [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
# How to use
```js
const { port1, port2 } = new MessageChannel();
const webSocket = new MessagePortWebSocket(port1);
webSocket.onmessage = event => {
// Could be either a string or Buffer
console.log(event.data);
};
webSocket.send('Hello, World!');
```
> Instead of subscribing to `onmessage`, you can also subscribe using `on('message', handler)`.
Note that when `MessagePortWebSocket` is constructed, the MessagePort is already established. So we assume Web Socket is already opened, thus, no `open` event will be emitted.
# Contributions
Like us? [Star](https://github.com/compulim/messageport-websocket/stargazers) us.
Want to make it better? [File](https://github.com/compulim/messageport-websocket/issues) us an issue.
Don't like something you see? [Submit](https://github.com/compulim/messageport-websocket/pulls) a pull request.