https://github.com/saghen/hermes
Generic interface for one and two way communication
https://github.com/saghen/hermes
Last synced: about 2 months ago
JSON representation
Generic interface for one and two way communication
- Host: GitHub
- URL: https://github.com/saghen/hermes
- Owner: Saghen
- License: mit
- Created: 2023-10-06T02:06:48.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-28T19:32:40.000Z (almost 2 years ago)
- Last Synced: 2025-08-14T03:52:44.005Z (about 2 months ago)
- Language: TypeScript
- Size: 86.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hermes
Hermes provides a generic interface for one way (Endpoints) and two way (Sockets) communication. It requires a transport for sending data between the server and client. The library provides transport implementations for HTTP (Endpoints only), Websocket, Web `postMessage`, Browser Extension `browser.runtime.sendMessage/port` and a loopback.
## Usage
```ts
import { createRouter, createEndpointClient, createSocketClient, Socket } from './src'
import { createLoopback, LoopbackRequestMetadata } from './src/transports/loopback'const loopback = createLoopback()
// Setup router
const endpoints = {
add: async (a: number, b: number) => a + b,
deep: {
echo: async (message: string) => message,
},
fail: () => Promise.reject('This endpoint fails'),
metadata: async (hello: 'world', metadata: LoopbackRequestMetadata) => hello,
}
const sockets = {
echo: async (socket: Socket) => {
for await (const message of socket.receiveIter()) {
await socket.send(message)
}
},
test: async () => {},
}
const router = createRouter(endpoints, sockets)
loopback.listen(router)// Setup clients
const endpointClient = createEndpointClient(loopback.endpointTransport)
const socketClient = createSocketClient(loopback.socketTransport)// Test endpoint client
await endpointClient.add(1, 2).then(console.log) // '3'
await endpointClient.deep.echo('foo').then(console.log) // 'foo'
await endpointClient.fail().catch(console.error) // 'This endpoint fails'
await endpointClient.metadata('world') // the second argument metadata passed by loopback transport// Test socket client
const socket = await socketClient.echo()
await socket.send('Hello world!')
await socket.receive().then(console.log) // 'Hello world'
```