Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexzhang1030/wshe
A simple yet modern websocket client.
https://github.com/alexzhang1030/wshe
typescript websocket websocket-client
Last synced: 7 days ago
JSON representation
A simple yet modern websocket client.
- Host: GitHub
- URL: https://github.com/alexzhang1030/wshe
- Owner: alexzhang1030
- License: mit
- Created: 2024-04-03T14:37:08.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-11-04T01:35:26.000Z (11 days ago)
- Last Synced: 2024-11-08T01:05:05.490Z (7 days ago)
- Topics: typescript, websocket, websocket-client
- Language: TypeScript
- Homepage:
- Size: 350 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wshe
**WS He**lper
A simple yet modern websocket client.
## Installation
```bash
pnpm i wshe
```## Usage
```ts
import { createWSHE } from 'wshe'const wshe = createWSHE('wss://echo.websocket.org', {
// Pass this to automatically connect on creation
immediate: true
})// You can also connect manually
wshe.connect()// You can send something
wshe.send('', '')// or send raw data
wshe.sendRaw('')
wshe.subscribeRaw((data) => {})// You can listen to messages
const unsubscribe = wshe.subscribe('', (payload) => {})
```## Server implementation
You should implement a server to transfer messages between clients.
And do this on `onmessage` to handle heartbeat requests:
```ts
import { getHeartbeatResponse, isHeartbeatRequest } from 'wshe/server'ws.onmessage = (message) => {
if (isHeartbeatRequest(message)) {
ws.send(getHeartbeatResponse(message))
return
}
// do your own logic here..., e.g.
JSON.parse(message)
}
```## Options
```ts
interface WSHEConfig {
/**
* onConnected event callback
* @default
* () => {}
*/
onConnected?: (ws: WebSocket, event: Event) => void
/**
* onDisconnected event callback
* @default
* () => {}
*/
onDisconnected?: (ws: WebSocket, event: CloseEvent) => void
/**
* onMessage event callback
* @default
* () => {}
*/
onError?: (ws: WebSocket, event: Event) => void/**
* Debugging log
* @default false
*/
debugging?: boolean
/**
* Connection ws immediately
* @default false
*/
immediate?: boolean
/**
* Heartbeat configuration
* @default
*/
heartbeat?: WSHEHeartbeatConfig/**
* Auto reconnect
* @default false
*/
autoReconnect?: boolean
}interface WSHEHeartbeatConfig {
/**
* @default 5000
*/
interval?: number
/**
* @default "ping"
*/
pingMessage?: string
/**
* @default "pong"
*/
pongMessage?: string
/**
* @default 10000
*/
timeout?: number
}
```## TypeScript Support
This library is written in TypeScript and provides good support for it.
Set message type will be like this:
```ts
const wshe = createWSHE<{
foo: {
bar: string
}
baz: {
qux: number
}
}>('...')wshe.subscribe('foo', (payload) => {
// ^? { bar: string }
})wshe.subscribe('baz', (payload) => {
// ^? { qux: number }
})
```## Credits
This project is highly inspired by [wsgo](https://github.com/melishev/wsgo).
## License
MIT