Skip to content

Commit

Permalink
feat: add ws client adapter (#319)
Browse files Browse the repository at this point in the history
Co-authored-by: Fran Mendez <[email protected]>
  • Loading branch information
Souvikns and fmvilas authored Dec 6, 2022
1 parent b4e3d9e commit 63c9cc2
Show file tree
Hide file tree
Showing 25 changed files with 24,680 additions and 7,586 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
examples/social-network/frontend
examples/crypto-websockets
dist
.glee
test
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
.DS_Store
dist
coverage
.vscode
.vscode
.glee
25 changes: 20 additions & 5 deletions docs/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default async function () {

This function must return an object with the following shape:


```js
export default async function () {
return {
Expand All @@ -26,7 +25,8 @@ export default async function () {

```

Here is an example of a `glee.config.js` file for reference:
Here is an example of a `glee.config.js` file for reference:

```js
export default async function () {
return {
Expand All @@ -35,6 +35,14 @@ export default async function () {
httpServer: customServer, // A custom HTTP server of your own.
adapter: "native", // Default. Can also be 'socket.io' or a reference to a custom adapter.
port: process.env.PORT,
},
client: {
authentication: {
token: process.env.TOKEN
},
query: {
foo: 'bar'
}
}
},
cluster: {
Expand All @@ -55,17 +63,24 @@ export default async function () {
};
}
```

Every protocol has different configuration needs so each protocol has unique configurations:

### Websocket Server

|Field|Description|
|--|--|
|websocket.server|Websocket server-specific configuration|
|websocket.server|Websocket server-specific configurations|
|websocekt.client|Websocket client-specific configurations|
|websocket.server.adapter| The Glee adapter to use for the WebSocket server. Defaults to a "native" WebSocket implementation. Other allowed values are `socket.io` (to use the [Socket.IO](https://socket.io/) Glee adapter) or a reference to a custom adapter.|
|websocket.server.httpServer| A custom HTTP server of your own. E.g., an [Express](https://expressjs.com/en/4x/api.html) server or any object that implements the [http.Server](https://nodejs.org/api/http.html#http_class_http_server) interface. |
|websocket.server.port| The port to use when binding the WebSocket server. This is useful when your server is behind a proxy and the port exposed for consumption is not the same as the port your application should be bound to. Defaults to the port specified in the selected AsyncAPI server.|
|websocket.client.authetication| Authentication variables for client|
|websocket.client.authentication.token| HTTP Authentication header|
|websocket.client.query| Query object for the client to send

### Cluster

### Cluster
|Field|Description|
|--|--|
|cluster.adapter| The Glee cluster adapter to use for communication between instances. Defaults to Redis Pub/Sub ("redis"). Can be a reference to a custom adapter.|
Expand All @@ -81,4 +96,4 @@ Every protocol has different configuration needs so each protocol has unique con
|mqtt.authentication.clientId| MQTT client Id for authentication
|mqtt.authentication.userPassword| username and password parameters for authentication|
|mqtt.authentication.userPassword.username| username parameter
|mqtt.authentication.userPassword.password| password parameter
|mqtt.authentication.userPassword.password| password parameter
4 changes: 4 additions & 0 deletions examples/crypto-websockets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Crypto Websockets
This example tries to showcase how Glee could be used as a WebSocket client. There is a server that streams the price change of a fake cryptocurrency (we can call it asyncapicoin) and the client subscribes to this stream and draws a graph in the terminal.

<img src="./graph.png">
1 change: 1 addition & 0 deletions examples/crypto-websockets/client/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TOKEN=arb-tokenValue
34 changes: 34 additions & 0 deletions examples/crypto-websockets/client/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
asyncapi: 2.4.0
info:
title: asyncapicoin client
version: 1.0.0
description: |
This app creates a client that subscribes to the server for the price change.
servers:
websockets:
url: ws://localhost:3000
protocol: ws
x-remoteServers:
- websockets
channels:
/price:
bindings:
ws:
bindingVersion: 0.1.0
publish:
operationId: index
message:
$ref: '#/components/messages/indexGraph'
components:
messages:
indexGraph:
payload:
type: object
properties:
status:
type: string
time:
type: number
price:
type: number

1 change: 1 addition & 0 deletions examples/crypto-websockets/client/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"time":1663935946665,"price":130,"status":"started"},{"time":1663935947684,"price":140,"status":"intransit"},{"time":1663935948690,"price":130,"status":"intransit"},{"time":1663935949698,"price":140,"status":"intransit"},{"time":1663935950710,"price":150,"status":"intransit"},{"time":1663935951716,"price":130,"status":"intransit"},{"time":1663935952724,"price":100,"status":"intransit"},{"time":1663935953737,"price":110,"status":"intransit"},{"time":1663935954742,"price":140,"status":"intransit"}]
27 changes: 27 additions & 0 deletions examples/crypto-websockets/client/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from 'path'
import fs from 'fs'
import asciichart from 'asciichart'

export default async function (event) {
const payload = event.payload
const dbPath = path.resolve('./db.json')
const read = () => JSON.parse(fs.readFileSync(dbPath, 'utf-8'))
const write = (data) => { fs.writeFileSync(dbPath, data, { encoding: 'utf-8' }) }
let db
switch (payload.status) {
case 'started':
write(JSON.stringify([payload]))
break
case 'intransit':
db = read()
write(JSON.stringify([...db, payload]))
break
case 'finished':
db = read()
const values = [...db, payload]
console.log(asciichart.plot(values.map(v => v.price), {height: 8}))
}
return {
send: []
}
}
165 changes: 165 additions & 0 deletions examples/crypto-websockets/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions examples/crypto-websockets/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "client",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "glee dev"
},
"type": "module",
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@asyncapi/glee": "file:../../..",
"asciichart": "^1.5.25"
}
}
10 changes: 10 additions & 0 deletions examples/crypto-websockets/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"allowJs": true,
"target": "es6",
"esModuleInterop": true,
"moduleResolution": "node",
"module": "commonjs",
"resolveJsonModule": true,
}
}
Binary file added examples/crypto-websockets/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions examples/crypto-websockets/server/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
asyncapi: 2.4.0
info:
title: asyncapicoin server
version: 1.0.0
description: |
This app is a dummy server that would stream the price of a fake cryptocurrency
servers:
websocket:
url: ws://localhost:3000
protocol: ws
channels:
/price:
bindings:
ws:
bindingVersion: 0.1.0
subscribe:
message:
$ref: '#/components/messages/indexGraph'
components:
messages:
indexGraph:
summary: Data required for drawing index graph
payload:
type: object
properties:
status:
type: string
time:
type: number
price:
type: number
Loading

0 comments on commit 63c9cc2

Please sign in to comment.