R
RunPod2w ago
xnerhu

Serverless handler on Nodejs

Hi. I see there is official SDK for serverless handler, but for Python. I don't see any API for handler in js-sdk.
4 Replies
3WaD
3WaD2w ago
That's correct. You write the serverless handler in Python.
xnerhu
xnerhuOP2w ago
still, is it possible to use nodejs? I'm not familiar with internal implementation of runpod serverless handler
3WaD
3WaD2w ago
It is. The thing running on the serverless can be whatever you want. You just have to expose it to the RunPod via the handler function in the Python script. The minimal code required is:
import runpod

def handler(job):
job_input = job["input"] # request parameters sent to RunPod are exposed via this
# Your code here.
return "your-job-results"

runpod.serverless.start({"handler": handler})
import runpod

def handler(job):
job_input = job["input"] # request parameters sent to RunPod are exposed via this
# Your code here.
return "your-job-results"

runpod.serverless.start({"handler": handler})
You can then connect your Nodejs code for example via HTTP API - by running a local server and sending internal requests to it from the Python script:
import requests
import json

def handler(job):
res = requests.post("http://localhost:3000", json=job["input"])
return res.json()
import requests
import json

def handler(job):
res = requests.post("http://localhost:3000", json=job["input"])
return res.json()
Or by spawning a subprocess
import subprocess
import json

def handler(job):
result = subprocess.run( # use .Popen() for streaming
["node", "script.js"],
input=json.dumps(job["input"]),
text=True,
capture_output=True
)
return json.loads(result.stdout)
import subprocess
import json

def handler(job):
result = subprocess.run( # use .Popen() for streaming
["node", "script.js"],
input=json.dumps(job["input"]),
text=True,
capture_output=True
)
return json.loads(result.stdout)
Other methods like Pipes, WebSockets, PubSub or direct TCP exist, but the communication method depends on your needs.
xnerhu
xnerhuOP2w ago
thank you for the example!

Did you find this page helpful?