obs_websocket

Some background, I needed a way to automate the start and stop streaming actions for OBS (Open Broadcast Software) with cron on OSX. This package will allow you to do that with dart or can be used with flutter to control OBS with a platform independent mbile app.

Getting Started

In your flutter project add the dependency:

dependencies:
  ...
  obs_websocket: ^1.0.0

For help getting started with dart, check out these guides.

Usage Example

Import the websocket connection library and the response library.

import 'package:obs_websocket/obs_websocket.dart';
import 'package:obs_websocket/response.dart';

Opening a websocket Connection

The WebSocket protocol, described in the specification RFC 6455 provides a way to exchange data between client and server via a persistent connection. The data can be passed in both directions as “packets”.

Before a websocket connection can be made to a running instance of OBS, you will need to have the obs-websocket plugin installed and configured.

To open a websocket connection, we need to create new ObsWebSocket using the special protocol ws in the url:

ObsWebSocket obsWebSocket = ObsWebSocket(connectUrl: "ws://[obs-studio host ip]:4444");

obs-studio host ip - is the ip address or host name of the computer running OBS that wou would like to send remote control commands to.

Authenticating to OBS

OBS has an optional, but highly recommended password security feature, the getAuthRequired method will check if the password security has been enabled. The AuthRequired object that the method call returns is used as part of the authentication process. The protocol documentation provided on the obs-websocket github pages covers this in detail.

final AuthRequired authRequired = await obsWebSocket.getAuthRequired();

if (authRequired.status)
	await obsWebSocket.authenticate(authRequired, "mySecretDontTell");

Sending Commands to OBS

The available commands are documented on the protocol page of the obs-websocket github page

SimpleResponse response await obsWebSocket.command("StartStreaming");

response.status will be true on success. response.error will give an error description if one is available.

Commands can also return a result as a Map:

SimpleResponse response = await obsWebSocket.command("GetSourcesList");

List sources = response.map["sources"];

sources.forEach((source) => print(source["name"] + " - " + source["type"]));

Additionally you can provide arguments with a command:

response = await obsWebSocket.command("GetSourceSettings", { "sourceName": "foreground" });

Map newSettings = Map<String,dynamic>.from(response.map);

newSettings["sourceSettings"]["height"] = 1080;
newSettings["sourceSettings"]["width"] = 1920;

response = await obsWebSocket.command("SetSourceSettings", newSettings);

print(response.map);

Closing the websocket

Finally before your code completes, you will need to close the websocket connection

obsWebSocket.close();

More documentation

The api docs will become available when I figure out how to publish them :-)

Known bugs

I've submitted a bug to the obs-websocket team for a bug that I am seeing when executing a websocket connecting program multiple times in sequence to start and stop steaming causes OBS to crash. For my use case I am able to work around this by stopping and restarting OBS itself before restarting streaming.

Libraries

obs_websocket
response