obs-websocket-js
OBSWebSocket.JS allows Javascript-based connections to the Open Broadcaster plugin obs-websocket.
Download | Samples | Changelog
Installation
npm install obs-websocket-js --save bower install obs-websocket-js --save
Typescript definitions are included in this package, and are automatically generated to match the latest obs-websocket
release.
Usage
Instantiation
The web distributable exposes a global named OBSWebSocket
.
In node...
const OBSWebSocket = ;
Create a new WebSocket connection using the following.
- Address is optional; defaults to
localhost
with a port of4444
. - Password is optional.
const obs = ;obs;
Sending Requests
All requests support the following two Syntax options where both err
and data
will contain the raw response from the WebSocket plugin.
Note that all response objects will supply both the original obs-websocket response items in their original format (ex: 'response-item'
), but also camelCased (ex: 'responseItem'
) for convenience.
- RequestName must exactly match what is defined by the
obs-websocket
plugin. {args}
are optional. Note that bothrequest-type
andmessage-id
will be bound automatically.- To use callbacks instead of promises, use the
sendCallback
method insetad ofsend
.
// Promise APIobs returns Promise // Callback APIobs no return value // The following are additional supported requests.obs returns Promiseobs;
Receiving Events
For all events, data
will contain the raw response from the WebSocket plugin.
Note that all response objects will supply both the original obs-websocket response items in their original format (ex: 'response-item'
), but also camelCased (ex: 'responseItem'
) for convenience.
- EventName must exactly match what is defined by the
obs-websocket
plugin.
obs; // The following are additional supported events.obs;obs;obs;obs;
Handling Errors
By default, certain types of WebSocket errors will be thrown as uncaught exceptions. To ensure that you are handling every error, you must do the following:
- Add a
.catch()
handler to every returned Promise. - Add a
error
event listener to theOBSWebSocket
object. By default only errors on the initial socket connection will be caught. Any subsequent errors will be emit here and will be considered uncaught without this handler.
// You must add this handler to avoid uncaught exceptions.obs;
Example
See more examples in \samples
.
const OBSWebSocket = ; const obs = ;obs ; obs; // You must add this handler to avoid uncaught exceptions.obs;
Debugging
To enable debug logging, set the DEBUG
environment variable:
# Enables debug logging for all modules of osb-websocket-js DEBUG=obs-websocket-js:* # on Windows set DEBUG=obs-websocket-js:*
If you have multiple libraries or application which use the DEBUG
environment variable, they can be joined with commas:
DEBUG=foo,bar:*,obs-websocket-js:* # on Windows set DEBUG=foo,bar:*,obs-websocket-js:*
Browser debugging uses localStorage
localStoragedebug = 'obs-websocket-js:*'; localStoragedebug = 'foo,bar:*,obs-websocket-js:*';
For more information, see the debug
documentation.
Upgrading from 1.x to 2.x
In order to better decouple the javascript library from the obs-websocket plugin the decision has been made to no longer provide method definitions for request/event methods. You are responsible for aligning your method calls with the plugin version that you would like to support.
// No longer supported.obs;obs; // Supported.obs;obs;
Upgrading from 2.x to 3.x
-
The
es5
build is no longer provided. If you're in an environment which must run ES5-compatible code, continue using the latest 2.x release. -
The Callback API has been separated from the Promise API. If you use callbacks in your
send
invocations, you will need to update them to use the newsendCallback
method:// No longer supported!obs;// Use this instead:obs; -
The
connect
method no longer accepts a callback. Use the promise it returns instead.// No longer supported!obs;// Use this instead:obs;
Projects Using obs-websocket-js
To add your project to this list, submit a Pull Request.