Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ws client adapter #319

Merged
merged 28 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
14f0554
feat: poc for ws client
Souvikns May 29, 2022
3d4329e
refactor: better ws example
Souvikns Jun 16, 2022
8d1ab0c
refactor: lint fix
Souvikns Jun 23, 2022
3d8ae26
feat: connect to ws servers
Souvikns Jun 23, 2022
4bfd839
feat: connect multiple channels
Souvikns Jun 28, 2022
4aea383
feat: create multiple ws clients for channels
Souvikns Jul 13, 2022
180f999
chore: lint fix
Souvikns Jul 13, 2022
025fa03
feat: change client priority
Souvikns Jul 14, 2022
2e83465
feat: sending message to ws client
Souvikns Aug 8, 2022
e727ef1
feat: new example
Souvikns Aug 22, 2022
3e9c039
Merge branch 'master' into ws-client
fmvilas Aug 22, 2022
d70118a
Remove .glee folders
fmvilas Aug 22, 2022
d6082cb
Fix bugs in lifecycle events
fmvilas Aug 22, 2022
17b0833
Fix compilation error on MQTT adapter
fmvilas Aug 22, 2022
6f33a24
Fix lifecycle function on crypto-websockets example
fmvilas Aug 22, 2022
d42c1f7
Add .glee directories to .gitignore
fmvilas Aug 22, 2022
621c34a
feat: finish crypto-websockets example & remove ws-server
Souvikns Aug 23, 2022
18e274a
refactor: lint fixes
Souvikns Aug 23, 2022
a54b960
refactor: clean dummy example
Souvikns Aug 24, 2022
8f97fdc
feat: add header and query support
Souvikns Aug 31, 2022
926f94e
refactor: lint fix
Souvikns Aug 31, 2022
2175485
feat: sending header and query values
Souvikns Sep 19, 2022
bb245c8
feat: add support for header and query values
Souvikns Sep 26, 2022
e754559
refactor: add requested changes
Souvikns Oct 12, 2022
4f56491
Merge remote-tracking branch 'upstream/master' into ws-client
Souvikns Nov 25, 2022
71e9399
feat: add x-remoteServers support
Souvikns Dec 1, 2022
7ccfba7
feat: authentication and query support
Souvikns Dec 1, 2022
cc9753c
Merge remote-tracking branch 'upstream/master' into ws-client
Souvikns Dec 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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