How to create an embedded viewer solution
Introduction
This guide explains how to build a viewer solution that can be embedded in and communicate with a website (e.g. the solution receives user inputs from a website). It’s part of a three step process to embed a Packhunt viewer solution into your website, so make sure you follow them step by step.
Prerequisites
- You have an allowed Packhunt app endpoint - see request Packhunt app endpoint.
1. Configure the viewer solution
Configure a solution that only shows a Viewer in the user interface.
- Create a
root-frame - Add a
Modelblock for the geometry - Add a
Viewerblock with the necessary materials for visualising the geometry - Subscribe the
Viewerto theModelresults - If deployed, there’s nothing to see nor test yet. Testing will be explained later in the guide.
kind: Solution
...
frames:
root-frame:
kind: Frame
contents:
my-geometry-model: ...
my-viewer: ...
2. Add an Embed block
Add an Embed block to the root frame to communicate with the host website (e.g. receive inputs from the website).
- Add an
Embedblock to yourroot-framecontent - Specify the
allowedOriginsas provided in the app endpoint request.
...
root-frame:
kind: Frame
contents:
my-geometry-model: ...
my-viewer: ...
my-embed:
kind: Embed
allowedOrigins:
- http://localhost:1234
- https://my-website-test.com
- https://my-website.com
3. Configure allowed messages
Configure messages sent from the website to the solution.
- Specify an
allowedMessageto set all inputs (coming from the website). Add all inputs as properties to the Embed block. By default they’re allrequired.
my-embed:
kind: Embed
allowedOrigins: ...
allowedMessages:
set-inputs:
description: "Set all inputs ..."
properties:
my-input-a:
type: Number
description: "This is ..."
my-input-b:
type: String
description: "This is ..."
...
- Subscribe the
Modelto the Emed block input message(s).
my-geometry-model:
kind: Model
modelFile: ...
subscribe:
- kind: Embed
source: my-embed
frame: root-frame
messageId: set-inputs
action: set
- (Optional) Specify an
allowedMessageto set partial inputs. Add all the model inputs as properties to the message, but set them all torequired: false. Then subscribe the Model to the Embed block withaction: update.
4. Test the solution without the website
Test the solution (so far) by sending user inputs to the solution via the route.
- Subscribe the
Embedblock to theRoute. - In the
setMessageproperty specify the name of theallowedMessageto which to send the route parameters. - In the
urlParametersspecify the message properties that will be set in the route.
my-embed:
kind: Embed
...
subscribe:
- kind: Route
urlParameters: [my-input-a, my-input-b]
setMessage: set-inputs
- Deploy the solution.
- Open the solution and add the url parameters to the route.
E.g.https://app.packhunt.io/my-project-slug?my-input-a=test&my-input-b=123 - Adjust the values in the route to test if the solution is working as expected.
5. Configure available events
Configure events sent from the solution to the website. Note that the outgoing messages can only be tested with a website.
- Specify outgoing
availableEventswith properties that match the model outputs. - Set the property to
nullable: trueif the content can benull.
my-embed:
kind: Embed
...
availableEvents:
my-geometry-model-warnings:
description: "User warnings from the geometry model."
properties:
my-warning-a:
type: String
description: "This warning is ..."
nullable: true
my-warning-b: ...
...
- Subscribe the
Embedblock to the model outputs.
my-embed:
kind: Embed
...
availableEvents:
my-geometry-model-warnings: ...
...
subscribe:
...
- kind: Model
frame: root-frame
source: my-geometry-model
eventID: my-geometry-model-warnings
Next
Once the viewer solution is ready you can continue with the next embedding guide - Setup your website.