Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use fal realtime client #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@fal-ai/serverless-client": "^0.6.0-alpha.4",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand Down
2 changes: 2 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.App {
text-align: center;
display: flex;
flex-direction: row;
}

.App-logo {
Expand Down
96 changes: 28 additions & 68 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,22 @@
import React, { useRef, useEffect, useCallback, useMemo, useState } from 'react';
import { debounce } from 'lodash';
import * as fal from '@fal-ai/serverless-client';
import { useEffect, useRef, useState } from 'react';

const DEBOUNCE_TIME = 0.1; // Adjust as needed
const URL = "wss://110602490-lcm-sd15-i2i.gateway.alpha.fal.ai/ws";
const APP_ID = '110602490-lcm-plexed-sd15-i2i';
const PROMPT = 'A moon in a starry night sky';
const CIRCLE_RADIUS = 80; // Radius of the circle

function App() {
const canvasRef = useRef(null);
const webSocketRef = useRef(null);
const isReconnecting = useRef(false);
const [receivedImage, setReceivedImage] = useState(null);

const connect = useCallback(() => {
webSocketRef.current = new WebSocket(URL);
webSocketRef.current.onopen = () => {
console.log('WebSocket Open');
};

webSocketRef.current.onclose = () => {
console.log('WebSocket Close');
};

webSocketRef.current.onerror = (error) => {
console.error("WebSocket Error:", error);
};

webSocketRef.current.onmessage = (message) => {
try {
const data = JSON.parse(message.data);
console.log("WebSocket Message:", data);
if (data.images && data.images.length > 0) {
setReceivedImage(data.images[0].url);
}
} catch (e) {
console.error("Error parsing the WebSocket response:", e);
const { send: sendCurrentData } = fal.realtime.connect(APP_ID, {
connectionKey: 'fal-realtime-example',
onResult: (result) => {
if (result.images && result.images[0]) {
setReceivedImage(result.images[0].url);
}
};
}, []);

const disconnect = useCallback(() => {
webSocketRef.current?.close();
}, []);

const sendMessage = useCallback(async (message) => {
if (!isReconnecting.current && webSocketRef.current?.readyState !== WebSocket.OPEN) {
isReconnecting.current = true;
connect();
}

if (isReconnecting.current && webSocketRef.current?.readyState !== WebSocket.OPEN) {
await new Promise((resolve) => {
const checkConnection = setInterval(() => {
if (webSocketRef.current?.readyState === WebSocket.OPEN) {
clearInterval(checkConnection);
resolve();
}
}, 100);
});
isReconnecting.current = false;
}
webSocketRef.current?.send(message);
}, [connect]);

const sendCurrentData = useMemo(() => {
return debounce(sendMessage, DEBOUNCE_TIME);
}, [sendMessage]);
},
});

useEffect(() => {
const canvas = canvasRef.current;
Expand All @@ -86,29 +38,37 @@ function App() {
const x = event.clientX - canvas.offsetLeft;
const y = event.clientY - canvas.offsetTop;
drawCircle(x, y);

const imageData = canvas.toDataURL('image/png');
const req = {
prompt: 'A moon in a starry night sky',
seed: Math.floor(Math.random() * 100),
sendCurrentData({
prompt: PROMPT,
seed: 11252023,
image_url: imageData,
sync_mode: true,
strength: 0.5,
};
sendCurrentData(JSON.stringify(req));
});
};

canvas.addEventListener('mousemove', handleMouseMove);

return () => {
canvas.removeEventListener('mousemove', handleMouseMove);
};
}, [connect, disconnect, sendCurrentData]);
}, []);

return (
<div className="App">
<canvas ref={canvasRef} style={{border: "1px solid black"}} width={700} height={500} />
{receivedImage && <img style={{border: "1px solid black"}} src={receivedImage} alt="Received from server" />}
<canvas
ref={canvasRef}
style={{ border: '1px solid black' }}
width={512}
height={512}
/>
<div style={{ border: '1px solid black', width: 512, height: 512 }}>
{receivedImage && (
<img src={receivedImage} alt="Received from server" />
)}
</div>
</div>
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import './index.css';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
</React.StrictMode>,
);

// If you want to start measuring performance in your app, pass a function
Expand Down
2 changes: 1 addition & 1 deletion src/reportWebVitals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const reportWebVitals = onPerfEntry => {
const reportWebVitals = (onPerfEntry) => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry);
Expand Down