By Tony Champion | May 2018 | Get the Code
In today’s world, building a successful app means moving beyond a single device. Users want apps that span all their devices and even connect with other users. Providing this type of experience can be a daunting challenge, to say the least. To help address this growing need in the ecosystem, Microsoft introduced Project Rome. Project Rome aims to create a more personal OS that spans apps, devices and users. While Project Rome has SDKs available for most major platforms, in this article I’m going to explore using Project Rome to create a team messaging Universal Windows Platform (UWP) app.Say Hello to Project Rome
Project Rome is an initiative to help you drive user engagement across apps and devices. It’s a collection of APIs that are part of Microsoft Graph and can be categorized into two areas: continue now and continue later.
The Remote System APIs enable an app to break beyond the boundaries of the user’s current device, enabling a continue-now experience. Whether allowing the user to use two devices for a single experience, as with a companion or remote-control app, or allowing multiple users to connect and share a single experience, these APIs provide an expanded view of the user’s current engagement. The team messaging app built in this article will create a shared user experience.
The other half of Project Rome, the Activities APIs, focuses on continuing the user’s experience at a later time. These APIs allow you to record and retrieve specific actions within your app that the user can continue from any device. Though I don’t discuss them in this article, these are definitely worth looking into.
Before diving into building the app, I first need to set up my environment. While the first version of Project Rome has been out for a little while, some of the features used in this article were just released during the recent Fall Creators Update. Therefore, your machine must be running build number 16299 or greater. At this point, this release is available in the slow ring for updates and most machines should be updated correctly.
Running apps with the Remote System APIs with other users requires that shared experiences are enabled on the machine. This can be done in the system settings, in Settings |System | Shared experiences. For the team messaging scenario, you need to enable different users to communicate with your device, which means you need to make sure shared experiences is enabled and that you can share or receive from “Everyone nearby,” as shown in Figure 1.
Figure 1 Enabling Shared Experiences
The final requirement is that your device be discoverable by having some level of connectivity. The Remote System APIs will discover other machines on the same network, as well as those nearby by using Bluetooth. Bluetooth can be enabled in the “Bluetooth and other device settings” page in your system settings.
With the machine set up, let’s begin by creating a new Visual C# app using the Blank App (Universal Windows) template in Visual Studio 2017. Call the app “TeamMessenger.” As mentioned earlier, this project requires the Fall Creators Update, so set the target and minimum versions of the app to “Build 16299” or greater, as shown in Figure 2. This will prevent the app from supporting older versions of Windows 10, but it’s necessary for some of the features touched on in this article.
Figure 2 Setting Target Versions for the App
Note that if you don’t have the Fall Creators update SDKs on your device, the easiest way to obtain them is to update Visual Studio 2017 to the latest release.
The Project Rome APIs are part of the Windows 10 SDK, which means there are no additional SDKs to download or NuGet packages to install in order to build this app. There are, however, a few capabilities that must be added to the app in order for the remote session APIs to work correctly. This can be done by opening the package.appxmanifest file and selecting the Capabilities tab. In the list of available capabilities, make sure the following are checked: Bluetooth, Internet (Client & Server) and Remote System.Building the Session Connection
This app will consist of two pages, with the first page responsible for creating or joining a remote session using the Remote System APIs. For simplicity, I’ll build this page using the MainPage.xaml that was created with the solution and is already wired into the app to be the first page loaded. The UI has two modes: creating or hosting a session and joining an existing session. Creating a session requires a session name that will be public to users looking to join. Joining an existing session needs to show a list of available nearby sessions. Both modes need a name to be displayed to the user. Figure 3 shows what the resulting UI should look like for MainPage, and the XAML to build this page can be found in Figure 4.
Figure 3 The MainPage UI
After creating the XAML for the page, create a new ViewModels folder in the app, then add a new public class in that folder called MainViewModel.cs. This view model will be wired into the view to handle the functionality.
The first portion of the view model will handle managing state for the radio buttons that determine if the user is creating a new session or joining an existing one. The state is held in a bool called IsNewSession. Two methods are used to switch the status of this bool, CreateSession and JoinSession:
The Checked event for each radio button is bound to one of these methods.
The remaining UI elements are tracked with simple properties. The session name and user name are bound to the SessionName and JoinName properties. The SelectedSession property binds to the SelectedItem property in the ListView and its ItemsSource is bound to the Sessions property:
The view model has two events that will be used to let the view know if a connection to a session was successful or not:
Finally, the Start button is bound to a Start method. This method can be left empty for the moment.
Once the view model has been completed, the codebehind for MainPage needs to create a public property that’s an instance of the MainViewModel. This is what allows x:Bind to build the compile-time bindings. In addition, it needs to subscribe to the two events created in the view model. If a connection is successfully made, I’ll navigate to a new page, MessagePage. If the connection fails, a MessageDialog will be displayed, informing the user of the failed connection. Figure 5 contains the code for the MainPage.xaml.cs.
Before digging in the heart of the app, you need to define a couple of data models that will be used in the app. Create a Models folder in the app and then create two classes in it: User and UserMessage. As the name suggests, the User model will track information about the users connected to the app:
The UserMessage class will contain the message content, the user creating the content and when the message was created:
With the code for the main page fairly complete, I can start building out RemoteSessionManager, which will be used to wrap the Remote Systems API. Add a new public class called RemoteSessionManager to the root directory of the app. The app will use a single shared instance of the RemoteSessionManager, so you can add a static property to the App class in App.xaml.cs:
Before the app can access any of the Remote Systems APIs, it must first obtain permission from the user. This permission is obtained by calling the static method RemoteSystem.RequestAccessAsync:
The method will return a RemoteSystemAccessStatus enum that can be used to determine if access was granted. This method must be called from the UI thread so it can successfully prompt the user. Once the user has granted or denied permission to the app, any subsequent calls will automatically return the user’s preference. For this app, this permission will be added to the session discovery because it’s called first in the workflow.
Note that all of the Remote System APIs can be found in the Windows.System.RemoteSystem namespace.
The first method to add to the RemoteSessionManager class is the CreateSession method. Because there are several results that can be returned from this method, I’ll wrap those up in a new enum—SessionCreationResult. SessionCreationResult has four possible values: success and three different failures. A session can fail to create because the user didn’t grant access to the app; the app currently has too many sessions running; or a system error failed to create the session:
Remote sessions are managed by a RemoteSystemSessionController. When creating a new RemoteSystemSessionController instance, you must pass in a name that will be displayed to devices attempting to join the session.
Once the controller is requested, a session can be started by calling the CreateSession method. This method returns a RemoteSystemSessionCreationResult containing a status and the new instance of the session if it was successful. The RemoteSessionManager will store the new controller and session in private variables.
A new public property, IsHost, should be added to the manager, as well as to determine workflow. During the CreateSession method, this value is set to true, identifying this app as the host. Another public property, CurrentUser, provides an instance of the user on the machine and will be used for messaging. The session manager also maintains an ObservableCollection of users in the current session. This collection is initialized with the newly created user. For the host of the session, this instance gets created in the CreateSession method. The resulting additions to the RemoteSessionManager are shown in Figure 6.
There are three more items that need to be added to RemoteSessionManager to complete the CreateSession method. The first is an event handler for when a user attempts to join a session and the JoinRequested event is raised on the session. The OnJoinRequested method will automatically accept any user trying to join. This could be extended to prompt the host for approval before the user is joined to the session. The request information is provided as a RemoteSystemSessionJoinRequest included in the RemoteSystemSessionJoinRequestedEventArgs parameter of the event handler. Invoking the Accept method will add the user to the session. The following code includes the new event to add to RemoteSessionManager, as well as the completed OnJoinRequested method:
The session manager can monitor when participants are added or removed from the current session through the RemoteSystemSessionParticipantWatcher. This class monitors the participants and raises either an Added or Removed event when needed. When a user joins a session already in progress, each participant already in the current session will receive an Added event. The app will take this series of events and determine which participant is the host by matching the DisplayName against the session’s ControllerDisplayName. This will allow participants to communicate directly with the host. The session manager maintains a participant watcher as a private variable that’s initialized in the InitParticipantWatcher. This method is called whether creating a session or joining an existing session. Figure 7 contains the new additions. You’ll notice that for this workflow you need to know when a participant is removed only if you’re the host, and if a participant is added if you’re joining a session. As a host, the RemoteSessionManager is concerned only when a participant leaves the session. The host will be notified directly by the participant when they join, as you’ll see later in the article. Participants only need to determine the current host account.
The last thing the CreateSession method needs added to the class is an event to let consumers know if and when the session is disconnected. The new SessionDisconnected event can be defined as follows:
Now that the app is able to broadcast a new remote session, the next thing to implement is the ability to join that session from another machine. There are two steps to joining a remote session: discovery and then connecting to the session.
Discovering a Session An app is able to discover nearby remote sessions through the RemoteSystemSessionWatcher, which is created from the static CreateWatcher method. This class raises events every time a session is added or removed. Add a new method—DiscoverSessions—to the RemoteSessionManager. This method will create a RemoteSystemSessionWatcher as a private variable to the class and handle the Added and Removed events. These events will be wrapped by two new events added to RemoteSessionManager: SessionAdded and SessionRemoved. Because this will be another entry point for users initializing remote sessions, you need to make sure to add a call to RemoteSystem.RequestAccessAsync. Figure 8 contains the private variable, the two events and the complete DiscoverSessions method.
It’s now possible to wire in the MainViewModel to update the Sessions property with locally available sessions. Because the DiscoverSessions method is asynchronous, the constructor of the MainViewModel needs to initialize a Task to invoke it. The initializing method should also register and handle the SessionAdded and SessionRemoved events. Because these events won’t be firing on the UI thread when updating the Sessions property, it’s important to use a CoreDispatcher. The updates to MainViewModel are in Figure 9.
Connecting to the Session Once a user has identified the session they want to join and supplies a name, the RemoteSessionManager must be able to connect them to the selected session. This will be handled by a new JoinSession method on the RemoteSessionManager, which takes the selected session and the entered display name as parameters.
The JoinSession method begins by calling the JoinAsync method on the supplied session. In return, this will fire the JoinRequested event on the host session. If the host approves the request, a success status is returned and the CurrentUser is set using the display name. As with the CreateSession method, the InitParticipantWatcher method is invoked to register an event handler for when participants are added to the session. The JoinSession method is shown in Figure 10.
The final step involved in joining a session is to use the functionality created in the RemoteSessionManager to either create or join a session. Figure 11 shows the Start method in the MainViewModel that’s bound to the Start button in MainPage. The workflow of the method is straightforward. Depending on IsNewSession, it calls either the CreateSession method or the JoinSession method. The results are returned by raising either the SessionConnected or ErrorConnecting events. If the session is successful, the app navigates to the MessagePage, which I’ll build in the next section.
At this point, the app can successfully create or join a session and has a messaging UI that’s ready to be used. The only remaining step is enabling the devices to communicate with each other. This is accomplished by using the Remote System API to send ValueSet instances between the machines. Each ValueSet is a set of key/value pairs of serialized payloads.
Receiving Messages Messages are transmitted within a session through a RemoteSystemSessionMessageChannel. A session can have multiple channels; however, this app will need only a single channel. In the RemoteSessionManager, I’m going to add a StartReceivingMessages method. This method will create a new message channel that’s stored in a private variable and then add a handler to the ValueSetReceived event.
Messages are sent as text and because the app is using classes as messages, I need to serialize the data. When the ValueSet is received from the channel, a DataContractJsonSerializer is used to rehydrate the message classes in the DeserializeMessage class. Because I’m unable to tell what type of message is serialized, the app will send each type of message as a different value in the ValueSet. The DeserializeMessage class will determine which key is used and return the correct class.
Once the message class is ready, the manager class will act on the message depending on its type. As you’ll see, participants will announce themselves to the host by sending their CurrentUser instance. In response, the host will broadcast the updated user list to all participants. If the session manager receives the list of participants, it will update the Users collections with the updated data. The final option, a UserMessage, will raise a new MessageReceived event that passes the message and the participant who sent the message. These additions to the RemoteSessionManager can be found in Figure 12.
Figure 12 includes a new event handler class, MessageReceivedEventArgs, that must also be created. This class contains two properties: the sender and the message:
Sending Messages The Remote Systems API provides two methods for delivering messages to other users. The first is to broadcast a message to all of the users in the session. This approach will be used for two of our message types, the UserMessage and the list of Users. Let’s create a new method, BroadcastMessage, in the RemoteSystemManager. This method takes a key and the message as parameters. Using a DataContractJsonSerializer, I serialize the data and use the BroadcastValueSetAsync method to send the message to all of the users, as shown in Figure 13.
The second approach is to send a message to a single participant. This approach is similar to broadcasting a message, except it uses the SendValueSetAsync method to message a participant directly. This final method to the RemoteSystemManager, SendMessage, can be found in Figure 14.
With the messaging now in place, it’s time to put it to use and finish the app. Add a new Blank Page to the app, MessagePage.xaml. This page will consist of a list of users, a message window and input fields for adding a message. The full XAML can be found in Figure 15.
Like MainPage, MessagePage will need a view model. Add a new class, MessageViewModel, to the ViewModels folder. This view model will need to support INotifyPropertyChanged to allow the two-way binding to function properly. This view model will contain three properties: Users, Messages and NewMessage. The Users will simply expose the RemoteSessionManager’s User collection to the view. Messages will be an ObservableCollection of UserMessage objects received and a NewMessage string containing the text to send as a new message. There is also a single event, MessageAdded, that will be used by the codebehind in MessagePage. In the constructor of the view model, I need to map the Users property, invoke the StartReceivingMessages method in RemoteSessionManager, and register for the MessageReceived event, as shown in Figure 16. The constructor also includes the implementation of INotifiyPropertyChanged.
In the constructor there’s a call to RegisterUser. This method will send the CurrentUser that was created when joining a session to the host. This announces to the host that a new user has joined and what the display name is. In response, the host will send out the current list of users to be displayed in the app:
The final piece of the view model is to broadcast a new message from the user. The SubmitMessage method constructs a new UserMessage and calls the BroadcastMessage method on the RemoteSessionManager. It then clears out the NewMessage value and raises the MessageAdded event, as shown in Figure 17.
In the codebehind for MessagePage, shown in Figure 18, I need to do two things: create an instance of the MessageViewModel for the XAML to reference and handle the MessageAdded event. In the event handler I instruct the ListView to scroll to the bottom of the list where the latest message is visible.
The Team Messaging app should now be ready to run. On one machine run the app and create a new session. Then launch the app on a second machine, which should show the newly created message. Once you join the session you’ll be brought to the new message page where you can begin chatting with others in the session, as shown in Figure 19. You have now created a multiuser app using the Remote System API.
Figure 19 Multiuser Messaging
Creating successful user experiences within apps often requires looking beyond a single device or platform or even user. Microsoft developed Project Rome to enable developers to provide this level of experience within their apps. In this article I built a UWP app using the Remote Systems API; however, by using the Project Rome SDKs available for other platforms, you could extend this app to work on multiple platforms. When building the next great experience for your users, remember to consider how Project Rome can help you make your app more personal. The source code for this article can be found at bit.ly/2FWtCc5.
Tony Champion is a software architect with more than 20 years of experience developing with Microsoft technologies. As the president of Champion DS and its lead software architect, he remains active in the latest trends and technologies, creating custom solutions on Microsoft platforms. His list of clients span multiple industries and includes companies such as: Schlumberger, Microsoft, Boeing, MLB and Chevron/Philips. Champion is an active participant in the community as a six-year Microsoft MVP, international speaker, published author and blogger.
Thanks to the following Microsoft technical expert who reviewed this article: Shawn Henry