Next.js Discord

Discord Forum

Running external program from Vercel Serverless Function

Unanswered
Encyrtid wasp posted this in #help-forum
Open in Discord
Encyrtid waspOP
Hello, I'm trying to build a c++ program that shares a repo with my Next.js app and then call it from a Serverless function.

I believe I have the c++ application built (according to the build logs) but I am not sure where I should place it to be called from the function endpoint. I have tried copying into a few different spots and also leaving it where it is but I don't know where the serverless function is rooted at or even if it's allowed to call external programs. It doens't seem to end up in the build anywhere

Anybody have any experience with this? Thanks in advance!

4 Replies

@Encyrtid wasp one option is WASM, which can be invoked from JS.
Vercel Serverless uses Node.js runtime, deploying to AWS Lambda.
Tiktoken is a wasm used in Next.js app, to count tokens for OpenAI chat api. It's CPU intensive code so need to be off-load to OS thread, otherwise Node.js single thread loop get choked.
https://github.com/dqbd/tiktoken
Tiktoken is in Rust impl btw
Encyrtid waspOP
Would that involve compiling the c++ program for WASM? I don’t know how portable that stuff is but at the moment we are using cmake. I also haven’t had any experience with WASM

I am the Next guy and a coworker is the one who wrote the other program so I don’t have much insight into how that was done
im a Rust dev so im not pretty sure but you can use emscripten for C++ codes
https://nodejs.dev/en/learn/nodejs-with-webassembly/#generating-webassembly-modules