Ruby clients for OBS Studio WebSocket v5.0
Go to file
2023-08-11 13:10:44 +01:00
examples Callbacks module extended 2023-07-27 14:55:00 +01:00
lib rename name to req_name in OBSWSRequestError 2023-08-11 13:10:44 +01:00
test add attr_reader r_client for OBSWSTest 2023-07-19 15:24:15 +01:00
.gitignore minor version bump 2023-07-19 15:14:46 +01:00
CHANGELOG.md initial commit 2022-10-22 22:30:40 +01:00
Gemfile initial commit 2022-10-22 22:30:40 +01:00
Gemfile.lock new error classes 2023-08-11 02:22:14 +01:00
LICENSE Initial commit 2022-10-22 21:47:31 +01:00
main.rb raekfile updated 2023-07-21 06:37:14 +01:00
obsws.gemspec observer dependency removed 2023-07-26 10:52:23 +01:00
Rakefile fix error in rakefile 2023-07-21 06:39:53 +01:00
README.md rename name to req_name in OBSWSRequestError 2023-08-11 13:10:44 +01:00

Gem Version License: MIT Ruby Code Style

Ruby Clients for OBS Studio WebSocket v5.0

Requirements

  • OBS Studio
  • OBS Websocket v5 Plugin
    • With the release of OBS Studio version 28, Websocket plugin is included by default. But it should be manually installed for earlier versions of OBS.
  • Ruby 3.0 or greater

Installation

Bundler

bundle add obsws
bundle install

Use

Example main.rb

Pass host, port and password as keyword arguments.

require "obsws"

class Main
  def run
    OBSWS::Requests::Client
      .new(host: "localhost", port: 4455, password: "strongpassword")
      .run do |client|
        # Toggle the mute state of your Mic input
        client.toggle_input_mute("Mic/Aux")
      end
  end
end

Main.new.run if $PROGRAM_NAME == __FILE__

Passing OBSWS::Requests::Client.run a block closes the socket once the block returns.

Requests

Method names for requests match the API calls but snake cased.

example:

# GetVersion
resp = r_client.get_version

# SetCurrentProgramScene
r_client.set_current_program_scene("BRB")

For a full list of requests refer to Requests

Events

Register on_ callback methods. Method names should match the api event but snake cased.

example:

class Observer
    def initialize
        @e_client = OBSWS::Events::Client.new(host: "localhost", port: 4455, password: "strongpassword")
        # register callback methods with the Event client
        @e_client.register(
          [
            method(:on_current_program_scene_changed),
            method(:on_input_mute_state_changed)
          ]
        )
    end

    # define "on_" event methods.
    def on_current_program_scene_changed(data)
        ...
    end
    def on_input_mute_state_changed(data)
        ...
    end
    ...
end

For a full list of events refer to Events

Attributes

For both request responses and event data you may inspect the available attributes using attrs.

example:

resp = cl.get_version
p resp.attrs

def on_scene_created(data):
    p data.attrs

Errors

If a general error occurs an OBSWSError will be raised.

If a connection attempt fails or times out an OBSWSConnectionError will be raised.

If a request fails an OBSWSRequestError will be raised with a status code.

  • The request name and code are retrievable through attributes {OBSWSRequestError}.req_name and {OBSWSRequestError}.code

For a full list of status codes refer to Codes

Logging

To enable logs set an environmental variable OBSWS_LOG_LEVEL to the appropriate level.

example in powershell:

$env:OBSWS_LOG_LEVEL="DEBUG"

Tests

To run all tests:

bundle exec rake -v

Official Documentation

For the full documentation: