WebSocket Example for Carl Davis's Dice Rollers

Before proceeding further, I'd like to point out I'm not an expert or even proficient in using node.js for applications yet. This is example is as-is and is based on samples found in online sources. Hopefully, it will work just fine for you as well.

To used the server follow the steps below:

First step is to install Node.js that can be downloaded from the Node.js SITE.

Open your terminal application and create a directory for your server and cd into this directory. Then execute this command:

npm init

You can accept the default answers for most of the questions. The name you give the application doesn't really matter.

Next step is to install the necessary modules for Web Sockets and the clipboard acces.

npm install ws
npm install clipboardy

Finally, create an index.js file with the following code (find the text file with this code HERE):

// Simple Server for taking input from the Stream Deck dice rollers. // There is a server that handles getting a push of the roll from the stream deck and one that handles // client requests for this data (for example a web page or another application that can do websockets) // import { WebSocketServer } from 'ws'; import clipboard from 'clipboardy'; // Current roll value var rollvalue = 0; // Hard coding ports - change as needed // wss is the socket that receives the roll value from the stream deck application // serverwss is the socket that handles client requests for the current roll value const wss = new WebSocketServer({ port: 25555 }); const serverwss = new WebSocketServer({port: 25556}); console.log("Starting Roll Receiver Server on Port 25555"); console.log("Starting Client Interface Server on Port 25556"); // Create the connection for the receiving roll value wss.on('connection', function connection(ws) { // On message received write the data to the clipboaord (to allow for pasting) and store the current roll value ws.on('message', function message(data) { console.log('received: %s', data); clipboard.writeSync(data.toString()); rollvalue = data.toString(); }); // Not really needed but I like to respond initiatlly at connection. ws.send('SUCCESS'); }); // Create the connection for the server to provide last roll value to a client. serverwss.on('connection', function connection(ws) { // Any message received will trigger returning the current value ws.on('message', function message(data) { console.log(`Client Request Received: ${data}; Sending value: ${rollvalue}`); // This is a design choice. For every client connected, the latest roll is sent regardless of who requested it. serverwss.clients.forEach(client => { //console.log(`distributing message: ${rollvalue}`) client.send(`${rollvalue}`) }); }); });

Before running the application, you will need to edit the package.json file to include: "type": "module". When you are done , the file will look similar to:

{
"dependencies": {
"clipboardy": "^4.0.0",
"ws": "^8.14.2"
},
"type": "module"
}

Now you are ready to run the server application:

node index.js

I also provide an example web page that uses the same server to retrieve the value of the latest roll and display it (this happens every second). This is very useful for displaying it on your computer screen of within an application that supports embedding web pages (suck as OBS stream software).

The example web page is Here.

About This Page

The Stream Deck Dice Roller from Carl Davis support a simple WebSocket interface. At the start of Stream Deck software, they connect via a web socket to 127.0.0.1:25555 (port is arbitrarily selected and, yes, should be configurable but isn't at this time) and begin sending the roll values when the buttons are pressed.

The data sent is only the value of the die roll. If you have a roller action setup and push the button only the roll result is sent.

This page shows a simple Node.js based Web Socket server which receives this data and places it on the clipboard (thus can then be pasted in other applications) and also provides a server that allows another application to request the latest value though a websocket. It provides a server that the stream deck application sends the value to and a server that can handle requests for the current value (on port 127.0.0.1:25556). Excellent for applications that can access a web socted to retrieve information. I include a sample web page that retrieves vallue and displays it. Great example that can be used as is in OBS (using the web page display).