Multi-user VR on the Web
Write full-featured multi-user VR experiences entirely in HTML.
Built on top of the wonderful A-Frame.
- Includes everything you need to create multi-user WebVR apps and games.
- Support for WebRTC and/or WebSocket connections.
- Voice chat. Audio streaming to let your users talk in-app (WebRTC only).
- Bandwidth sensitive. Only send network updates when things change. Option to further compress network packets.
- Extendable. Sync any A-Frame component, including your own, without changing the component code at all.
- Cross-platform. Works on all modern Desktop and Mobile browsers. Oculus Rift, HTC Vive and Google Cardboard + Daydream support.
- Firebase WebRTC signalling support
Follow the NAF Getting Started tutorial to build your own example from scratch, including setting up a local server.
To run the examples on your own PC:
git clone https://github.com/networked-aframe/networked-aframe.git # Clone the repository.cd networked-aframenpm install && npm run easyrtc-install # Install dependencies.npm run dev # Start the local development server.
With the server running, browse the examples at
http://localhost:8080. Open another browser tab and point it to the same URL to see the other client.
For info on how to host your experience on the internet, see the NAF Hosting Guide.
My Networked-Aframe Scene
Open in two tabs if nobody else is online.
- Basic with 4 clients
- Dance Club
- Google Blocks
- Tracked Controllers
- Positional Audio
- Dynamic Room Name
- Form to set room and username
- Minecraft Clone
Made something awesome with Networked-Aframe? Let me know and I'll include it here!
Networked-Aframe works by syncing entities and their components to connected users. To connect to a room you need to add the
networked-scene component to the
a-scene element. For an entity to be synced, add the
networked component to it. By default the
rotation components are synced, but if you want to sync other components or child components you need to define a schema. For more advanced control over the network messages see the sections on Broadcasting Custom Messages and Options.
Required on the A-Frame
|serverURL||Choose where the WebSocket / signalling server is located.||/|
|app||Unique app name. Spaces are not allowed.||default|
|room||Unique room name. Can be multiple per app. Spaces are not allowed. There can be multiple rooms per app and clients can only connect to clients in the same app & room.||default|
|connectOnLoad||Connect to the server as soon as the webpage loads.||true|
|onConnect||Function to be called when client has successfully connected to the server.||onConnect|
|adapter||The network service that you wish to use, see adapters.||wseasyrtc|
|audio||Turn on / off microphone audio streaming for your app. Only works if the chosen adapter supports it.||false|
|debug||Turn on / off Networked-Aframe debug logs.||false|
networked-scene will connect to your server automatically. To prevent this and instead have control over when to connect, set
connectOnLoad to false in
networked-scene. When you are ready to connect emit the
connect event on the
To disconnect simply remove the
networked-scene component from the
a-scene from your page will also handle cleanly disconnecting.
Creating Networked Entities
<!-- Attach local template by default --><!-- Do not attach local template -->
Create an instance of a template to be synced across clients. The position and rotation will be synced by default. The
aframe-lerp-component is added to allow for less network updates while keeping smooth motion.
Templates must only have one root element. When
attachTemplateToLocal is set to true, the attributes on this element will be copied to the local entity and the children will be appended to the local entity. Remotely instantiated entities will be a copy of the root element of the template with the
networked component added to it.
<!-- Locally instantiated as: --><!-- Remotely instantiated as: -->
<!-- No changes to local entity on instantiation --><!-- Remotely instantiated as: -->
|template||A css selector to a template tag stored in
|attachTemplateToLocal||Does not attach the template for the local user when set to false. This is useful when there is different behavior locally and remotely.||true|
Deleting Networked Entities
Currently only the creator of a network entity can delete it. To delete, simply delete the element from the HTML using regular DOM APIs and Networked-Aframe will handle the syncing automatically.
Syncing Custom Components
By default, the
rotation components on the root entity are synced.
To sync other components and components of child entities you need to define a schema per template. Here's how to define and add a schema:
Components of the root entity can be defined with the name of the component. Components of child entities can be defined with an object with both the
selector field, which uses a standard CSS selector to be used by
document.querySelector, and the
component field which specifies the name of the component. To only sync one property of a multi-property component, add the
property field with the name of the property.
Once you've defined the schema then add it to the list of schemas by calling
Component data is retrieved by the A-Frame Component
data property. During the network tick each component's data is checked against its previous synced value; if the data object has changed at all it will be synced across the network.
Syncing nested templates - eg. hands
To sync nested templates setup your HTML nodes like so:
<a-entity id="player" networked="template:#player-template;attachTemplateToLocal:false;" wasd-controls><a-entity camera look-controls networked="template:#head-template;attachTemplateToLocal:false;"></a-entity><a-entity hand-controls="left" networked="template:#left-hand-template"></a-entity><a-entity hand-controls="right" networked="template:#right-hand-template"></a-entity></a-entity>
In this example the head/camera, left and right hands will spawn their own templates which will be networked independently of the root player. Note: this parent-child relationship only works between one level, ie. a child entity's direct parent must have the
Sending Custom Messages
Subscribe and unsubscribe callbacks to network messages specified by
dataType. Broadcast data to all clients in your room with the
broadcastData functions. To send only to a specific client, use the
sendData functions instead.
|clientId||ClientId to send this data to|
|dataType||String to identify a network message.
|callback||Function to be called when message of type
|data||Object to be sent to all other clients|
Transfer Entity Ownership
The owner of an entity is responsible for syncing its component data. When a user wants to modify another user's entity they must first take ownership of that entity. The ownership transfer example and the toggle-ownership component show how to take ownership of an entity and update it.
Take ownership of an entity.
Check if you own the specified entity.
Events are fired when certain things happen in NAF. To subscribe to these events follow this pattern:
Events need to be subscribed after the document.body element has been created. This could be achieved by waiting for the document.body
onLoad method, or by using NAF's
onConnect function. Use the NAF Events Demo as an example.
List of events:
|clientConnected||Fired when another client connects to you||
|clientDisconnected||Fired when another client disconnects from you||
|entityCreated||Fired when a networked entity is created||
|entityDeleted||Fired when a networked entity is deleted||
The following events are fired on the
networked component. See the toggle-ownership component for examples.
List of ownership transfer events:
|ownership-gained||Fired when a networked entity's ownership is taken||
|ownership-lost||Fired when a networked entity's ownership is lost||
|ownership-changed||Fired when a networked entity's ownership is changed||
NAF can be used with multiple network libraries and services. An adapter is a class which adds support for a library to NAF. If you're just hacking on a small project or proof of concept you'll probably be fine with the default configuration and you can skip this section. Considerations you should make when evaluating different adapters are:
- How many concurrent users do you need to support in one room?
- Do you want to host your own server? Or would a "serverless" solution like Firebase do the job?
- Do you need audio (microphone) streaming?
- Do you need custom server-side logic?
- Do you want a WebSocket (client-server) network architecture or WebRTC (peer-to-peer)?
I'll write up a post on the answers to these questions soon (please bug me about it if you're interested).
By default the
wsEasyRtc adapter is used, which is an implementation of the open source EasyRTC library that only uses the WebSocket connection. To quickly try WebRTC instead of WebSockets, change the adapter to
easyrtc, which also supports audio. If you're interested in contributing to NAF a great opportunity is to add support for more adapters and send a pull request.
List of the supported adapters:
|Adapter||Description||Supports Audio||WebSockets or WebRTC||How to start|
|wsEasyRTC||DEFAULT - EasyRTC that only uses the WebSocket connection||No||WebSockets||
|uWS||Custom implementation of uWebSockets||No||WebSockets||See naf-uws-adapter|
|Firebase||Firebase for WebRTC signalling||No||WebRTC||See naf-firebase-adapter|
|Deepstream||DeepstreamHub for WebRTC signalling||No||WebRTC||See naf-deepstream-adapter|
audio: true to the
networked-scene component (and using an adapter that supports it) you will not hear any audio by default. Though the audio will be streaming, it will not be audible until an entity with a
networked-audio-source is created. The audio from the owner of this entity will be emitted in 3d space from that entities position. The
networked-audio-source component must be added to an entity (or a child of an entity) with the
To quickly get started, try the Glitch NAF Audio Example.
Returns true if a connection has been established to the signalling server.
Returns the list of currently connected clients.
Frequency the network component
sync function is called, per second. 10-20 is normal for most Social VR applications. Default is
By default when an entity is created the
aframe-lerp-component is attached to smooth out position and rotation network updates. Set this to false if you don't want the lerp component to be attached on creation.
Compress each sync packet into a minimized but harder to read JSON object for saving bandwidth. Default is
To measure bandwidth usage, run two clients on Chrome and visit chrome://webrtc-internals
Stay in Touch
- Follow Hayden on Twitter
- Follow changes on GitHub
- Join the A-Frame Slack and add the #networked-aframe channel
- Let us know if you've made something with Networked-Aframe. We'd love to see it!
Help and More Information
- Getting started tutorial
- Edit live example on glitch.com
- Live demo site
- EasyRTC WebRTC library
- Bugs and requests can be filed on GitHub Issues
- Licenses and package information
- Packaged source code for deployment
- Server code
- Client source code
- Unit tests
Interested in contributing? Shoot me a message or send a pull request.
NAF is not supported on nodejs version 7.2.0. Please use a different version of nodejs.
This program is free software and is distributed under an MIT License.