streaming client support missing in websocket nodejs-ws-template.
li-tianshu opened this issue · 3 comments
In order to leverage asyncapi for existing websocket services, the following supports are needed in nodejs-ws-template:
- given a yaml specification, code generator needs to generate client that can connect to the websocket service and receive data in json format.
For example, given the below websocket service URL (that streams some data to the client once connected):
ws://admin:welcome@host1:port1/services/v2/stream1?begin=now
we can define a yaml API specification like below (note username password basic-auth and query parameter can be supported in different ways and this yaml is for demonstration only)
asyncapi: '2.2.0'
info:
title: some data streaming API
version: '1.0.0'
description: |
The demo API allows clients to subscribe to data
license:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0'
servers:
myserver1:
url: admin:Welcome_1@host1:port1
protocol: ws
defaultContentType: application/json
channels:
/services/v2/stream1:
bindings:
ws:
bindingVersion: 0.1.0
query:
type: object
description: query parameter like begin=now
properties:
begin:
type: string
default: now
description: begin position to stream data
subscribe:
summary: data records
operationId: onRecords
message:
name: records
payload:
type: object
description: array of data records in json format
With this yaml definitioin for the websocket service, code generator should be able to generate client code in different languages such as node-js or python. Below is a sample node-js client code
const WebSocket = require('ws')
const serviceURL = 'ws://admin:welcome@host1:port1/services/v2/stream1?begin=now'
// generic data process with the websocket service
const processData = (wsClient) => {
wsClient.on('message', function message(data) {
console.log('received some data:')
const records = eval(data.toString())
for (var i = 0; i < records.length; i++) {
console.log(records[i]);
}
});
wsClient.on('error', (err) => {
console.log(err.toString());
});
}
const init = async () =>{
const wsClient = new WebSocket(serviceURL);
// now streaming data
processData(wsClient)
}
Here is an example client code in python in similar flow
#!/usr/bin/env python
import asyncio
import requests
import websockets
import json
async def processData():
serviceURL = "ws://admin:welcome@host1:port1/services/v2/stream1?begin=now"
async with websockets.connect(serviceURL) as websocket:
print("start streaming data")
while True:
data = await websocket.recv()
records = json.loads(data)
for rec in records:
print("< {rec}")
asyncio.get_event_loop().run_until_complete(processData())
Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.
There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.
Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.
Thank you for your patience ❤️
closing as this is being implemented as new template -> https://github.com/tianshu-orcl/websocket-streaming-template