This project provides a proxy backend that connects the client to the Langflow API. It serves as an intermediary to facilitate communication between the WebSocket-based client and the Langflow API, handling requests and streaming responses efficiently.
- Establishes WebSocket connections with clients.
- Proxies client requests to the Langflow API.
- Streams real-time responses back to the client.
- Handles errors gracefully.
- Uses
express
for API endpoints andws
for WebSocket connections. - Configurable via environment variables.
Ensure you have the following installed:
- Node.js (v22 or later)
- npm (Node Package Manager)
- A Langflow API endpoint and authorization token
-
Clone the repository:
git clone https://github.com/your-repo/proxy-backend-langflow.git cd proxy-backend-langflow
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory:APPLICATION_TOKEN=your_langflow_api_token PORT=3000
The server is built using express
for HTTP endpoints and ws
for WebSocket support.
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const cors = require('cors');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
app.use(cors());
app.use(express.json());
Each WebSocket connection is assigned a unique requestId
for tracking and communication.
const connections = new Map();
wss.on('connection', (ws) => {
const requestId = Math.random().toString(36).substring(7);
connections.set(requestId, ws);
ws.on('close', () => {
connections.delete(requestId);
});
ws.send(JSON.stringify({ type: 'requestId', requestId }));
});
Provides a basic health check for the server.
app.get('/', (req, res) => {
res.send('Hello World');
});
Handles requests from clients, forwards them to the Langflow API, and streams responses.
app.post('/chat', async (req, res) => {
const { input_value, requestId } = req.body;
const ws = connections.get(requestId);
if (!ws) {
return res.status(400).json({ error: 'WebSocket connection not found' });
}
try {
const response = await axios.post(
'https://api.langflow.astra.datastax.com/lf/<path>/api/v1/run/<path>?stream=false',
{
input_value,
output_type: 'chat',
input_type: 'chat',
tweaks: {
// Your tweaks here
}
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.APPLICATION_TOKEN}`
}
}
);
const message = response.data.outputs[0].outputs[0].results.message.text;
ws.send(JSON.stringify({ type: 'response', message }));
res.json({ status: 'Processing' });
} catch (error) {
ws.send(JSON.stringify({ type: 'error', message: error.message }));
res.status(500).json({ error: error.message });
}
});
The server listens on the port specified in the .env
file or defaults to 3000
.
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
-
Start the server:
node server.js
-
Establish a WebSocket connection to the server:
const ws = new WebSocket('ws://<server-ip>:3000'); ws.onmessage = (event) => { const data = JSON.parse(event.data); console.log(data); };
-
Send chat requests via the
/chat
endpoint:curl -X POST http://<server-ip>:3000/chat \ -H 'Content-Type: application/json' \ -d '{"input_value": "Hello", "requestId": "your_request_id"}'
-
Client Connection:
- The client connects via WebSocket to the server and receives a unique
requestId
.
- The client connects via WebSocket to the server and receives a unique
-
Request Handling:
- The client sends a POST request with
input_value
andrequestId
to the/chat
endpoint.
- The client sends a POST request with
-
Proxy Forwarding:
- The server forwards the request to the Langflow API with necessary headers and payload.
-
Response Streaming:
- The server receives a response from the Langflow API and streams it back to the client using WebSocket.
-
Error Management:
- Errors are caught and relayed to the client via WebSocket and HTTP responses.
APPLICATION_TOKEN
: Authorization token for Langflow API.PORT
: Port to run the server (default: 3000).
express
ws
axios
cors
dotenv
Install them using:
npm install express ws axios cors dotenv
This project is licensed under the MIT License. See the LICENSE file for details.