###########
SSR support
###########

HOOPS Communicator supports two rendering modes: Client and Server. Although our front end has infrastructure for creating SSR enabled viewers via the drop-down menu, our backend application is not setup to handle that case. In this section we will remedy that situation.

If you examine the ``createNewViewer`` method, you will see that we are sending JSON data containing the value of the ``renderer-type`` dropdown to our ``Spawner`` application. In order to create SSR instances we will need to add some code to *spawner.js* to process this data.

The first step is to add some middleware to our ``Spawner`` class that will parse incoming requests as json objects. Add the following line of code in the ``Spawner.start`` method right before the code that installs the CORS middleware function:

.. code-block:: js

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

Now that our incoming requests are parsed as json, we can check to see if the renderer type is SSR when we go to spawn a viewer. Add the following code to the ``spawnViewer`` method righter after the line that hard codes ``rendererType`` to "csr":
	
.. code-block:: js

	const requestJson = req.body;
	if ("rendererType" in requestJson) {
	  rendererType = requestJson.rendererType;
	}

The final piece of the puzzle is to inform ``ts3d_sc_server`` that it should start in SSR mode. That can be accomplished by using the ``--ssr`` command line option. Add the following code after the ``args`` array is created in the ``spawnViewer`` method:

.. code-block:: js

	if (spawnInfo.rendererType == "ssr") {
	  args.push("--ssr", "true");
	}

Finally, for SSR to work on UNIX systems, the DISPLAY environment variable should be set. This generally will have no effect on Windows based systems, so for the purposes of this tutorial we will set it in all cases. Replace the existing call to ``spawn`` with the following code:
	
.. code-block:: js

	const spawnOptions = {env: {"DISPLAY": ":0"}};
	let process = spawn(this.executablePath, args, spawnOptions);

At this point. If you restart the server and select "Server" from the renderer type dropdown menu you will notice that the created viewer is running in SSR mode.

In a production environment, it is not recommended to expose communication with the Spawning server directly to users of your webpage. You would most likely want to have a system in the middle that can execute business logic, pull files out of cloud storage, perform authentication, or any other number of tasks before granting a user the ability to provision limited server resources.
