What is the name of the domain?
local
What is the issue you’re encountering
Experiencing message events upon initial Cloudflare AI Gateway non-realtime WebSocket API connection, but subsequent requests don’t yield further message events despite a persistent WebSocket connection?
What steps have you taken to resolve the issue?
import dotenv from “dotenv”
import express from “express”;
import WebSocket from “ws”;
dotenv.config()
const api_token = process.env.GOOGLE_AI_STUDIO_TOKEN || “”;
const account_id = process.env.ACCOUNT_ID
const gateway_name = process.env.GATEWAY_NAME;
const cf_token = process.env.CF_TOKEN ;
const app = express();
const port = process.env.PORT || 8080;
app.use(express.json());
let ws: WebSocket;
const html = `
Cloudflare AI Gateway
Cloudflare AI Gateway
Send
console.log(“served from server”);
const idInput = document.getElementById(“idInput”);
const msgInput = document.getElementById(“msgInput”);
const sendButton = document.getElementById(“send”);
sendButton.addEventListener("click", () => {
const id = idInput.value;
const message = msgInput.value;
const api = fetch("/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id, message }),
});
api
.then((response) => {
if (response.ok) {
console.log("Message sent successfully");
return response.json();
} else {
console.error("Error sending message");
}
})
.then((data) => {
console.log("Response:", data);
})
});
</script>
</body>
</html>
const wssConnect = () => { // websocket connection // non realtime ws = new WebSocket(
wss://gateway.ai.cloudflare.com/v1/${account_id}/${gateway_name}, { headers: { "cf-aig-authorization":
Bearer ${cf_token}`,
},
},
);
ws.on("open", () => {
console.log("Connected to server.")
});
ws.on("message", function incoming(message) {
console.log("Received message ....");
const msg = message.toString();
console.log(msg);
});
ws.on('error', () => {
console.log("Something wrong.")
});
ws.on('close', () => {
console.log('disconnected');
});
}
const send = (id:string, question:string) => {
console.log(“Sending request…”);
console.log("Websocket ready state = ", ws.readyState)
ws.send(
JSON.stringify({
type: “universal.create”,
request: {
eventId: id,
provider: “google-ai-studio”,
endpoint: “v1beta/models/gemini-2.0-flash:generateContent”,
headers: {
“x-goog-api-key”: ${api_token}
,
“Content-Type”: “application/json”,
},
query: {
“contents”: [
{
“role”:“user”,
“parts”: [ {“text”: question}]
}
]
},
},
}),
);
}
app.listen(port, () => {
console.log(Server is running at http://localhost:${port}
);
wssConnect();
});
app.get(“/”, (req, res) => {
res.send(html);
});
app.post(“/send”, (req, res) => {
console.log(req.body)
const {id, message} = req.body;
console.log(id = ${id}, message = ${message}
);
send(id, message);
res.json({ message: “text received”});
});