Skip to content

Commit f5d5f15

Browse files
authored
Merge pull request #38 from isamu/stream
Stream
2 parents bb7bd6b + e8dbab4 commit f5d5f15

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CallableRequest, CallableResponse } from "firebase-functions/v2/https";
2+
3+
export const sleep = async (milliseconds: number) => {
4+
return await new Promise((resolve) => setTimeout(resolve, milliseconds));
5+
};
6+
export const streamingFunc = async (request: CallableRequest, response?: CallableResponse) => {
7+
for await (const num of ["1", "2", "3", "4", "5"]) {
8+
// res.write(num + "\n");
9+
response?.sendChunk({ num });
10+
await sleep(1000);
11+
}
12+
13+
return {num: "12334"};
14+
};

functions/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ import exportIfNeeded from "./common/exportifneeded";
1515
exportIfNeeded("hono_server", "server/hono", exports);
1616
exportIfNeeded("express_server", "server/express", exports);
1717
exportIfNeeded("express_server_v2", "server/express_v2", exports);
18+
19+
exportIfNeeded("streamingCall", "server/streaming", exports);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { onCall } from "firebase-functions/v2/https";
2+
3+
import { streamingFunc } from "../../functions/server/streaming";
4+
5+
export default onCall(
6+
{
7+
region: "asia-northeast1",
8+
timeoutSeconds: 10,
9+
},
10+
async (request, response) => {
11+
return await streamingFunc(request, response);
12+
});

src/utils/functions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import { functions } from "@/utils/firebase";
22
import { httpsCallable } from "firebase/functions";
33

44
export const testFunctions = httpsCallable(functions, "test");
5+
export const streamingFcuntion = httpsCallable(functions, "streamingCall");

src/views/MyPage.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44
<script lang="ts">
55
import { defineComponent } from "vue";
66
import { requireLogin } from "@/utils/utils";
7+
import { streamingFcuntion } from "@/utils/functions";
78
89
export default defineComponent({
910
setup() {
1011
requireLogin("/account");
1112
13+
(async () => {
14+
const { stream, data } = await streamingFcuntion.stream();
15+
for await (const chunk of stream) {
16+
console.log(chunk);
17+
}
18+
const allData = await data;
19+
console.log(allData);
20+
})()
21+
1222
return {};
1323
},
1424
});

0 commit comments

Comments
 (0)