It happens to all of us... More frequently as we age. ![:wink: :wink:](https://community.hubitat.com/images/emoji/apple/wink.png?v=12)
Sure.. I had to solve this issue very recently. You'll have to adapt it to work for you, but feel free to use this code however you wish. It has basic connection recovery but might need a little more polish.
index.js
/**
- HubConnect Automatic Proxy
- Copyright 2019 Steve White, Retail Media Concepts LLC.
- HubConnect for Hubitat is a software package created and licensed by Retail Media Concepts LLC.
- HubConnect, along with associated elements, including but not limited to online and/or electronic documentation are
- protected by international laws and treaties governing intellectual property rights.
- This software has been licensed to you. All rights are reserved. You may use and/or modify the software.
- You may not sublicense or distribute this software or any modifications to third parties in any way.
- By downloading, installing, and/or executing this software you hereby agree to the terms and conditions set forth in the HubConnect license agreement.
- http://irisusers.com/hubitat/hubconnect/HubConnect_License_Agreement.html
- Hubitat is the trademark and intellectual property of Hubitat, Inc. Retail Media Concepts LLC has no formal or informal affiliations or relationships with Hubitat.
- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
- on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License Agreement
- for the specific language governing permissions and limitations under the License.
*/
const appVersion = {platform: "NodeJS", major: 1, minor: 0, build: 0}
var WebSocket = require('ws');
const config = require('./config.json');
console.log(`HubConnect-Automatic Websocket Proxy Server v${appVersion.major}.${appVersion.minor}.${appVersion.build}\nCopyright 2019-2020 Retail Media Concepts LLC\nAll Rights Reserved\n\n`);
console.log("Initializing connections...\n\n")
// HubConnect Websocket Server
socketServer = new WebSocket.Server({port: config.port, perMessageDeflate: false});
socketServer.connectionCount = 0;
socketServer.on('connection', function(socket,request)
{
socketServer.connectionCount++;
console.log(
`Hubitat: ${socket._socket.remoteAddress}; ${socketServer.connectionCount} connected.`
);
socket.on('close', function(code, message)
{
socketServer.connectionCount--;
console.log(
`Hubitat: Hub disconnected; (${socketServer.connectionCount} connected)`
);
});
});
// Automatic Cloud Listener
var io = require('socket.io-client')
function automatic()
{
console.log (`Automatic: Connecting to the Automatic cloud websocket...`);
var autoSocket = io(`https://stream.automatic.com?token=${config.client_id}:${config.client_secret}`)
// Handle error messages
autoSocket.on('error', function(errorMessage)
{
console.log('Automatic: Error', errorMessage);
});
// Notify console of the connection success
autoSocket.on('connect', function()
{
console.log('Automatic: Connected to the Automatic cloud!');
});
// Handle disconnect messages
autoSocket.on('disconnect', function(errorMessage)
{
console.log('Automatic: Lost connection to Automatic cloud! Retrying in 30 seconds...', errorMessage);
setTimeout(automatic, 30000);
});
}
automatic();
function sendAutomaticEvent(eventType, eventData)
{
socketServer.clients.forEach(function each(client)
{
if (client.readyState === WebSocket.OPEN)
{
client.send(JSON.stringify({source: eventType, data: eventData}));
}
});
}
config.json
{
"client_id": "e4060ac1f42c658346ed",
"client_secret": "c4973cf67de9887c0ff5367bb2c7413f90d5b97b",
"port": 22200
}
package.json
{
"name": "automatic",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": ,
"author": "",
"license": "ISC",
"dependencies": {
"socket.io-client": "^2.3.0"
}
}
Hope this can help.