Stream Notes 11/25/19 - Adventures with Rails, JS, and OBS Websockets continued
Tags: rails, javascript, devjournal
Published: 2019-11-27 23:16:37

### Check Out the Stream @ [https://twitch.tv/talk2megooseman](https://twitch.tv/talk2megooseman)

## Recap

- Starting weaving in JavaScript connection for OBS Websocket
- Implement subscribing to specific OBS event using callbacks
- Discovered bug with OBS Websocket captions implementation, 2 second delay on all captions
- Refactor Rails Action Cable connection to make more sense
- Use MessageChannel to send and receive message at the global level for our vanilla JS

# Today Objective

- Continue work on integration OBS Websocket JavaScript into our [stream-cc.gooseman.codes](http://stream-cc.gooseman.codes) application to take advantage of Captions

# Notes

### OBS-Websocket-JS :(

Using the JavaScript code for the OBS Socket found in [https://github.com/t2t2/obs-tablet-remote/blob/master/src/obs-remote.js](https://github.com/t2t2/obs-tablet-remote/blob/master/src/obs-remote.js). 
Tried using the NPM package from [https://github.com/haganbmj/obs-websocket-js](https://github.com/haganbmj/obs-websocket-js) but that had major issues with compiling is the rest of my code. 

### Adjusting Action Cable Channels

After testing that and getting that idea out of the way next was to try to implement further integrations and actually send the closed-captioning text being sent out back to the UI for sending to OBS.

This required some changes to how I was using websocket channels on the server, I was overloading the speech channel with transaction information. To make this better to understand I created a new channel dedicated to transaction message to the user an adjusted code.

### Send Captions

Once changes were made to publish the closed-captioning text I would be sending to Twitch back to the UI also, I could then send it also to OBS using the `SendCaptions`.

**BUT** a bug was found! SendCaptions is using the wrong text channel to send closed captioning text to OBS. Thanks to @RatWithAShotgun, they had pointed out that the current implementation is using a text channel that will cause delay text by at least 2 seconds.

Here is a snippet of the offending code from [https://github.com/Palakis/obs-websocket/blob/da9dd6f77544308f35dbe0b716c3bcf7d97913c3/src/WSRequestHandler_Streaming.cpp#L310](https://github.com/Palakis/obs-websocket/blob/da9dd6f77544308f35dbe0b716c3bcf7d97913c3/src/WSRequestHandler_Streaming.cpp#L310)
```c++
    if (output) {
    		const char* caption = obs_data_get_string(req->data, "text");
    		obs_output_output_caption_text1(output, caption); // This line is the issue
    }
```
I think I will making a PR into OBS Websocket in the coming week once I can get all the dependecies build for testing.

### Keep on Trucking

After that little let down kept on trucking and implemented event subscription for when Stream/Recording has stopped to support feature set. Also added in a callback to get list of the user's Scenes and callback for when the user changes Scenes to support more feature ideas.

The last thing wanted to tackle at the end of the stream was broadcasting event coming from OBS Websocket listeners to the rest of the JS code. This was easily achievable using MessageChannel [http://developer.mozilla.org/en-US/docs/Web/API/MessageChannel](http://developer.mozilla.org/en-US/docs/Web/API/MessageChannel). Think of its exactly as an eventing library but NATIVE. This will help communication across different parts of the JS since were not using any UI libraries or state management.

# Future Action Items

Continue work on the JavaScript integration for OBS Websocket

- List and allow use to select scenes they want to be muted on
- Pause CC when selected scene is active
- Stop CC when stream stop triggers
- Look into why Captions are so delayed for the event