events-backend: add hub http subscription API + listen by subscription ID

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-05-22 18:34:56 +02:00
parent a90ce4aa39
commit ee52e38a04
2 changed files with 96 additions and 16 deletions
+41 -8
View File
@@ -79,6 +79,39 @@ backend.add(
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'events',
});
const subRes = await fetch(`${baseUrl}/hub/subscriptions/123`, {
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
topics: ['test'],
}),
});
console.log(
`DEBUG: sub create req = ${subRes.status} ${subRes.statusText}`,
);
const poll = async () => {
const res = await fetch(`${baseUrl}/hub/subscriptions/123`, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
const data = res.status === 200 && (await res.json());
console.log(
`DEBUG: sub poll req = ${res.status} ${res.statusText}`,
data,
);
poll();
};
poll();
const ws = new WebSocket(`${baseUrl}/hub/connect`, {
headers: {
Authorization: `Bearer ${token}`,
@@ -86,14 +119,14 @@ backend.add(
});
ws.onopen = () => {
console.log('DEBUG: ws.onopen');
ws.send(
JSON.stringify([
'req',
1,
'subscribe',
{ id: 'derp', topics: ['test'] },
]),
);
// ws.send(
// JSON.stringify([
// 'req',
// 1,
// 'subscribe',
// { id: 'derp', topics: ['test'] },
// ]),
// );
setTimeout(() => {
console.log(`DEBUG: publish!`);
ws.send(