obs_websocket 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

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.

1.0.0 #

  • included example matches the README and addition of bin/ws-obs.app command lne app for OSX

0.0.2 #

  • initial simplified support for responses

0.0.1 #

  • initial commit, no support for responses to commands

example/example.dart

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

void main(List<String> args) async {
  ObsWebSocket obsWebSocket = ObsWebSocket(connectUrl: "ws://localhost:4444");

  final AuthRequired authRequired = await obsWebSocket.getAuthRequired();

  SimpleResponse response;

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

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

  print(response.status);

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

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

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

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

  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);

  obsWebSocket.close();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  obs_websocket: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:obs_websocket/obs_websocket.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
40
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Analysis suggestions

Package not compatible with runtime flutter-web on web

Because:

  • package:obs_websocket/obs_websocket.dart that imports:
  • package:web_socket_channel/io.dart that imports:
  • dart:io

Package not compatible with runtime js

Because:

  • package:obs_websocket/obs_websocket.dart that imports:
  • package:web_socket_channel/io.dart that imports:
  • dart:io

Health suggestions

Format lib/obs_websocket.dart.

Run dartfmt to format lib/obs_websocket.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
crypto ^2.1.4 2.1.5
meta ^1.1.8 1.2.2 1.3.0-nullsafety
web_socket_channel ^1.1.0 1.1.0
Transitive dependencies
async 2.4.2
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
stream_channel 2.0.0
typed_data 1.2.0 1.3.0-nullsafety