Skip to content

Stream #38

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

Merged
merged 2 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"logs": "firebase functions:log"
},
"engines": {
"node": "18"
"node": "20"
},
"main": "lib/index.js",
"dependencies": {
Expand Down
14 changes: 14 additions & 0 deletions functions/src/functions/server/streaming.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CallableRequest, CallableResponse } from "firebase-functions/v2/https";

export const sleep = async (milliseconds: number) => {
return await new Promise((resolve) => setTimeout(resolve, milliseconds));
};
export const streamingFunc = async (request: CallableRequest, response?: CallableResponse) => {
for await (const num of ["1", "2", "3", "4", "5"]) {
// res.write(num + "\n");
response?.sendChunk({ num });
await sleep(1000);
}

return {num: "12334"};
};
2 changes: 2 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ import exportIfNeeded from "./common/exportifneeded";
exportIfNeeded("hono_server", "server/hono", exports);
exportIfNeeded("express_server", "server/express", exports);
exportIfNeeded("express_server_v2", "server/express_v2", exports);

exportIfNeeded("streamingCall", "server/streaming", exports);
12 changes: 12 additions & 0 deletions functions/src/wrappers/server/streaming.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { onCall } from "firebase-functions/v2/https";

import { streamingFunc } from "../../functions/server/streaming";

export default onCall(
{
region: "asia-northeast1",
timeoutSeconds: 10,
},
async (request, response) => {
return await streamingFunc(request, response);
});
1 change: 1 addition & 0 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import { functions } from "@/utils/firebase";
import { httpsCallable } from "firebase/functions";

export const testFunctions = httpsCallable(functions, "test");
export const streamingFcuntion = httpsCallable(functions, "streamingCall");
10 changes: 10 additions & 0 deletions src/views/MyPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
<script lang="ts">
import { defineComponent } from "vue";
import { requireLogin } from "@/utils/utils";
import { streamingFcuntion } from "@/utils/functions";

export default defineComponent({
setup() {
requireLogin("/account");

(async () => {
const { stream, data } = await streamingFcuntion.stream();
for await (const chunk of stream) {
console.log(chunk);

Check warning on line 16 in src/views/MyPage.vue

View workflow job for this annotation

GitHub Actions / lint_test (22.x)

Unexpected console statement
}
const allData = await data;
console.log(allData);

Check warning on line 19 in src/views/MyPage.vue

View workflow job for this annotation

GitHub Actions / lint_test (22.x)

Unexpected console statement
})()

return {};
},
});
Expand Down
Loading