Access OBS Studio from Web

Hi,

I am using Next Js for my frontend what I am trying to achieve whenever the user clicks the start Recording button it should be built the obs-web socket connection and streaming should start.
below is the code

import React, { useState, useEffect } from 'react';
import OBSWebSocket from 'obs-websocket-js';
const OBS_WS_SERVER = 'ws://192.168.29.134:4455';
const OBS_WS_PASSWORD = 'jx7oLhenRhfYiykQ';
const Obs = () => {
const [connected, setConnected] = useState(false);
const [recording, setRecording] = useState(false);
useEffect(() => {
const connectToOBS = async () => {
const obs = new OBSWebSocket();
try {
const { obsWebSocketVersion, negotiatedRpcVersion } = await obs.connect(
OBS_WS_SERVER,
OBS_WS_PASSWORD,
{ rpcVersion: 1 }
);
console.log(`Connected to server ${obsWebSocketVersion} (using RPC ${negotiatedRpcVersion})`);
setConnected(true);
} catch (error) {
console.error('Failed to connect', error);
setConnected(false);
}
};
connectToOBS();
}, []);
const handleStartRecording = async () => {
if (connected) {
try {
await Obs.call( {
requestType: 'StartStreaming',
});
setRecording(true);
console.log('Recording started');
} catch (error) {
console.error('Failed to start recording', error);
}
}
};
return (
<>
<h3>OBS Connection</h3>
{connected ? (
<>
<p>Connected to OBS</p>
<button onClick={handleStartRecording} disabled={recording}>
{recording ? 'Recording...' : 'Start Recording'}
</button>
</>
) : (
<p>Failed to connect to OBS</p>
)}
</>
);
};
export default Obs;
My websocket connection is established but when I click the button I get the error
client.js:1 Failed to start recording Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
 
Top