https://github.com/babiabeo/deko
A simple WebSocket client for Deno
https://github.com/babiabeo/deko
autobahn-testsuite deno rfc6455 websocket websocket-client
Last synced: about 1 month ago
JSON representation
A simple WebSocket client for Deno
- Host: GitHub
- URL: https://github.com/babiabeo/deko
- Owner: babiabeo
- License: mit
- Created: 2024-03-02T11:47:32.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-16T13:51:41.000Z (over 2 years ago)
- Last Synced: 2025-02-24T11:49:36.758Z (over 1 year ago)
- Topics: autobahn-testsuite, deno, rfc6455, websocket, websocket-client
- Language: TypeScript
- Homepage: https://jsr.io/@babia/deko
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deko
[](https://deno.land/std)
**_Deko_** is a simple WebSocket client for Deno.
> [!WARNING]
> If you want to connect WebSocket on browsers, use
> [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
> instead.
## Features
- [x] Easy to use.
- [x] Supports custom headers.
- [x] Passes the
[Autobahn testsuite](https://github.com/crossbario/autobahn-testsuite). [^report]
- [x] Follows [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455)
WebSocket implementation.
## Usage
This package is available on [jsr.io](https://jsr.io).
Use `deno add` command to add this package to your project:
```
deno add @babia/deko
```
Then, import it in your source file:
```ts
import { Deko } from "@babia/deko";
```
Or use `jsr:` specifier if you want to use an install step.
```ts
import { Deko } from "jsr:@babia/deko@^0.1.2";
```
## Examples
#### Open event handle
```ts
import { Deko, OpCode } from "@babia/deko";
const client = new Deko({ uri: "websocket url goes here" });
client.onOpen = () => {
console.log("Connected to WebSocket server!");
};
await client.connect();
```
#### Receives a text message from WebSocket server
```ts
import { Deko, OpCode } from "@babia/deko";
const client = new Deko({ uri: "websocket url goes here" });
client.onMessage = (_, message) => {
if (message.opcode === OpCode.TextFrame) {
console.log(new TextDecoder().decode(message.payload));
}
};
await client.connect();
```
#### Sends a message to WebSocket server
```ts
import { Deko, OpCode } from "@babia/deko";
const client = new Deko({ uri: "websocket url goes here" });
await client.connect();
setTimeout(async () => {
await client.sendMessage("Hello World!");
}, 5000); // Sends message after 5 seconds
```
## License
This repository/package is under **MIT License**. See [LICENSE](./LICENSE).
[^report]: https://babiabeo.github.io/autobahn/deko/