feat(realtime): introduce resultType="json"#180
Merged
Conversation
feae3ba to
2a7c9c1
Compare
2a7c9c1 to
5ec7a4f
Compare
Contributor
Author
|
I guess we could also introduce just Let me add that too... Or maybe just do it as a handler option, hm... |
Contributor
Author
|
introduced decodeMessage, encodeMessage, feels more appropriate and natural. Open to feedback. |
Contributor
Author
|
So like for json: import { createRealtimeClient, createConfig } from "@fal-ai/client";
const jsonCodec = {
encodeMessage: (input: any) =>
typeof input === "string" ? input : JSON.stringify(input),
decodeMessage: async (data: any) => {
const toText = async () => {
if (typeof data === "string") return data;
if (data instanceof Uint8Array) return new TextDecoder().decode(data);
if (data instanceof ArrayBuffer)
return new TextDecoder().decode(new Uint8Array(data));
if (data instanceof Blob)
return new TextDecoder().decode(new Uint8Array(await data.arrayBuffer()));
return String(data);
};
return JSON.parse(await toText());
},
};
const client = createRealtimeClient({
config: createConfig({ credentials: process.env.FAL_KEY!, fetch }),
});
const conn = client.connect("your-app-id", {
...jsonCodec,
onResult: (msg) => console.log("result", msg),
onError: (err) => console.error("error", err),
});
// send plain JS objects; will be JSON-stringified
conn.send({ prompt: "hello json" });
// later
// conn.close();and just to spice it up, say for protobuf (no need for protobuf, purely illustrative): import { createRealtimeClient, createConfig } from "@fal-ai/client";
import { MyMessage } from "./generated/my_message"; // your protobufjs static module
const protobufCodec = {
encodeMessage: (input: any) => {
// expects input already shaped like MyMessage
return MyMessage.encode(MyMessage.create(input)).finish(); // Uint8Array
},
decodeMessage: async (data: any) => {
const toBytes = async () => {
if (data instanceof Uint8Array) return data;
if (data instanceof ArrayBuffer) return new Uint8Array(data);
if (data instanceof Blob) return new Uint8Array(await data.arrayBuffer());
throw new Error("Unsupported payload for protobuf decode");
};
return MyMessage.decode(await toBytes());
},
};
const client = createRealtimeClient({
config: createConfig({ credentials: process.env.FAL_KEY!, fetch }),
});
const conn = client.connect("your-app-id", {
...protobufCodec,
onResult: (msg) => {
// msg is a decoded protobuf message
console.log("proto result", msg);
},
onError: (err) => console.error("proto error", err),
});
// send a protobuf-shaped object
conn.send({ prompt: "hello proto" });
// conn.close(); |
43db98b to
164ac55
Compare
drochetti
approved these changes
Jan 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Alternative to #178