https://github.com/mudssrali/wsgrok
WebSocket API wrapper for creating and managing WebSocket connections
https://github.com/mudssrali/wsgrok
real-time websocket websocket-api wss
Last synced: 11 months ago
JSON representation
WebSocket API wrapper for creating and managing WebSocket connections
- Host: GitHub
- URL: https://github.com/mudssrali/wsgrok
- Owner: mudssrali
- License: mit
- Created: 2025-01-26T17:06:55.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-04T07:49:04.000Z (12 months ago)
- Last Synced: 2025-03-04T07:55:16.162Z (12 months ago)
- Topics: real-time, websocket, websocket-api, wss
- Language: TypeScript
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WSgrok
WebSocket API wrapper for creating and managing WebSocket connections
[](https://standardjs.com)
## Install
Install with npm or yarn via
```
yarn add wsgrok
```
or
```
npm i wsgrok
```
## API
```ts
type Event = "connect" | "disconnect" | "message" | "verify"
interface WSgrok {
url: string
ready: boolean
ws?: WebSocket
pingInterval?: number
pending: Map any
reject: (a: any) => any
}>
messageTimeout: number
connectionVerified: boolean
verify(): void
connect(): Promise
on(event: Event, f: Function): void
onNext(event: Event, f: Function): void
cleanup(): void
ping(): void
send(message: any, timeout?: number): Promise
}
```
## Usage
- Use `WSgrok` with events
```ts
import WSgrok from 'wsgrok'
const wsgrok = new WSgrok("wss://example.com/ws")
// on connected to host
wsgrok.on('connect', () => console.log("connected to a host"))
// on disconnected from a host
wsgrok.on('disconnect', () => console.log("disconnected from a host"))
// on receiving a new message from host
wsgrok.on('message', (message: any) => console.log("new message", message))
// on verify a connection to host
wsgrok.on('verify', () => console.log("connection is verified"))
```
- Make a request to host using `wsgrok.Send()` with `Promise`
```ts
import WSgrok from 'wsgrok'
export const invokeService = () => {
const wsgrok = new WSgrok("wss://www.example.com/ws")
wsgrok.send({
type: "INVOKE_SERVICE",
clientType: "XNGINX"
payload: {
initiatorId: "xxxx-xxxx-xxxx"
reqToken: "xxxx-xxxx-xxxx"
}
})
.then(response => {
// do your work here
})
.catch(error => {
// do your work here
})
}
```
`Or with async arrow style`
```ts
import WSgrok from 'wsgrok'
export const invokeService = aysnc () => {
const wsgrok = new WSgrok("wss://www.example.com/ws")
try {
const response = await wsgrok.send({
type: "INVOKE_SERVICE",
clientType: "XNGINX",
payload: {
initiatorId: "xxxx-xxxx-xxxx"
reqToken: "xxxx-xxxx-xxxx"
}
})
// handle server response here
} catch(error) {
// handle server error here
}
}
```