# Error while building our own streaming server

**URL:** <https://forum.techsoft3d.com/t/error-while-building-our-own-streaming-server/216>\
**Category:** HOOPS Visualize Web\
**Created:** [August 24, 2020, 8:15am UTC](https://forum.techsoft3d.com/t/error-while-building-our-own-streaming-server/216 "2020-08-24T08:15:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ys878645](https://avatars.discourse-cdn.com/v4/letter/y/c5a1d2/32.png) [@ys878645](https://forum.techsoft3d.com/u/ys878645)\
**Post date:** [August 24, 2020, 8:15am UTC](https://forum.techsoft3d.com/t/error-while-building-our-own-streaming-server/216/1 "2020-08-24T08:15:16Z")

</div>

Hi i am currently trying to create our own streaming server as provided in the tutorial below.

[https://docs.techsoft3d.com/communicator/latest/build/tutorials/building-streaming-service/setup.html](https://docs.techsoft3d.com/communicator/latest/build/tutorials/building-streaming-service/setup.html)

I have passes the absolute path to paths.js directory  
licenseFile: “xxxxxxxxx”,  
executablePath: “E:/Received/HOOPS\_Communicator\_2020\_SP1\_Windows/HOOPS\_Communicator\_2020\_SP1/server/bin/win64/ts3d\_sc\_server.exe”,

modelDirectory: “E:/Received/HOOPS\_Communicator\_2020\_SP1\_Windows/HOOPS\_Communicator\_2020\_SP1/tutorials/server\_tutorial/models/helloworld.scz”  
};

engine-wasm.js:428 WebSocket connection to ‘ws://localhost:9999/’ failed: Error in connection establishment: net::ERR\_CONNECTION\_REFUSED

what coukd possibly be the problem…  
thanks in advance for any help .

---

<div class="post-metadata">

**Author:** ![rtadlock](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/rtadlock/32/14_2.png) [@rtadlock](https://forum.techsoft3d.com/u/rtadlock)\
**Post date:** [August 31, 2020, 4:21pm UTC](https://forum.techsoft3d.com/t/error-while-building-our-own-streaming-server/216/2 "2020-08-31T16:21:39Z")

</div>

Hi @ys878645, can you please show me the full command you used to launch the ts3d\_sc\_server.exe? The error makes me think that the stream cache server is not running, or it’s not running on the port you tried to connect to (9999).

Thanks and sorry for the delay in response here.

---

<div class="post-metadata">

**Author:** ![ys878645](https://avatars.discourse-cdn.com/v4/letter/y/c5a1d2/32.png) [@ys878645](https://forum.techsoft3d.com/u/ys878645)\
**Post date:** [February 1, 2021, 11:01am UTC](https://forum.techsoft3d.com/t/error-while-building-our-own-streaming-server/216/3 "2021-02-01T11:01:38Z")

</div>

its say spawn id exited with code 0.

this is the link i am following - [Introduction](https://docs.techsoft3d.com/communicator/latest/build/tutorials/building-streaming-service/introduction.html)  
Here is the code

const { spawn } = require(‘child\_process’);

const { v4: uuidv4 } = require(‘uuid’);

const express = require(‘express’);

class ServerInfo {

constructor(serverId, wsPort, rendererType, response) {

```
this.serverId = serverId;

this.wsPort = wsPort;

this.response = response;

this.rendererType = rendererType;

this.lastPingTime = null;

this.startTime = new Date();

```

}

}

class streamCacheServer {

constructor(licenseFile, executablePath, modelDirectory, expressApp) {

```
this.licenseFile = licenseFile;

this.executablePath = executablePath;

this.modelDirectory = modelDirectory;

this.expressApp = expressApp;

this.serverPort = null;

this.wsBasePort = 55000;

this.serverInfo = null;

this.availableWsPorts = [];

this.activeSpawns = new Map();

```

}

start(port) {

```
this.serverPort = port;

this.expressApp.use(express.json());

this.expressApp.use(function (req, res, next) {

  res.header("Access-Control-Allow-Origin", "*");

  res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");

  res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers");

  next();

});

this.expressApp.post("/loadModel", this.startViewer.bind(this));

this.expressApp.post("/liveliness/:serverId", this.liveliness.bind(this));

this.expressApp.listen(this.serverPort);

```

}

startViewer(req, res) {

```
let rendererType = "csr";

const requestJson = req.body;

if ("rendererType" in requestJson) {

  rendererType = requestJson.rendererType;

}

const serverId = uuidv4();

const wsPort = this.availableWsPorts.length ? this.availableWsPorts.pop() : this.wsBasePort++;

this.serverInfo = new ServerInfo(serverId, wsPort, rendererType, res);

this.activeSpawns.set(serverId, this.serverInfo);

let args = [

  "--id", this.serverInfo.serverId,

  "--sc-port", this.serverInfo.wsPort,

  '--liveliness-endpoint', `http://localhost:${this.serverPort}/liveliness`,

  "--model-search-directories", this.modelDirectory,

  "--license-file", this.licenseFile

];

if (this.serverInfo.rendererType == "ssr") {

  args.push("--ssr", "true");

}

const serverOptions = { env: { "DISPLAY": ":0" } };

let process = spawn(this.executablePath, args, serverOptions);

process.on("exit", (code) => {

  console.log(`spawn ${this.serverInfo.serverId} exited with code: ${code}`);

});

res.status(200).json({result: `spawned viewer on websocket port: ${this.serverInfo.wsPort}`});

```

}

liveliness(req, res) {

```
const serverId = req.params.serverId;

if (!this.serverInfo) {

  console.log(`unknown id: ${serverId}`);

  res.status(404).json({ error: "invalid serverId." });

  return;

}

const updateType = req.query.liveliness;

switch (updateType) {

  case "ready":

    console.log(`liveliness ready: ${this.serverInfo.serverId}`);

    this.serverInfo.response.status(201).json({

      directory: this.modelDirectory,

      id: this.serverInfo.serverId,

      endpoint: `ws://localhost:${this.serverInfo.wsPort}`,

      rendererType: this.serverInfo.rendererType

    });

    this.serverInfo.response = null;

    break;

  case "disconnect":

    console.log(`liveliness disconnect: ${this.serverInfo.serverId}`);

    this.availableWsPorts.push(this.serverInfo.wsPort);

    this.activeSpawns.delete(this.serverInfo.spawnId);

    res.status(200).end();

    break;

  case "ping":

    this.serverInfo.lastPingTime = new Date();

    res.status(200).end();

    break;

  default:

    res.status(400).json({ error: "invalid liveliness value" });

}

```

}

}

module.exports = { streamCacheServer }
