Tutorial: an agent with a camera
Feeder is an agent that watches a bird feeder through a camera of its own. In this tutorial you build its view, a page that renders the frames of that camera live and lets the person chat with Feeder and send it a photo taken with their own camera. The finished view also fits a phone screen while the on-screen keyboard is open.
What you will build
The finished view runs below. Ask Feeder something and then show it a bird with your camera or with the sample picture.
The fake agent of the example fails your first message on purpose (failNext: true) to show the Retry button.
What you will learn
- Show an agent together with the camera frames that arrive in its
data. - Follow frames that change ten times a second with
onDatawithout re-rendering the whole view. - Chat with the agent through
messages,sendandcanSend. - Open the camera of the person with
askForandgetUserMediaand fall back on a sample picture when there is no camera. - Take a photo on a canvas and send it with
send({ image, text }). - Fit the view to a phone with the keyboard height and the safe areas that
webappreports. - Handle what goes wrong: an agent that cannot be reached, a message that is not delivered and an error of the webapp.
Before you start
You need to know how a view connects and re-renders on every change (Quick start). Because the view of an agent reads a name, data and messages like the view of a world, you can start here without Tutorial: the view of a world.
Every step has a complete example in React and in plain JavaScript. Each example runs without an account or a network on a fake agent defined in the same file. You can copy the code of any step into your project and continue from there.
The steps
- Show the agent and its camera:
connectToHostwith a fake agent and the frames indata. - Follow the pictures of the camera:
onData, a frame counter and a pause button. - Talk with the agent: the conversation and a box where the person writes.
- Open the person's camera:
askFor,getUserMediaand a sample picture. - Take a photo and send it: a canvas and
send({ image, text }). - Fit the phone screen: the keyboard and the safe areas.
- Finish the view: the status of the agent, delivery and retry, the errors of
sendand two final touches.
Next steps
Start with step 1: Show the agent and its camera.