PMA.UI is a Javascript library that provides UI and programmatic components to interact with Pathomation ecosystem. It’s extended interoperability allows it to display WSI slides from PMA.core, PMA.start or Pathomation’s cloud service My Pathomation. PMA.UI can be used in any web app that uses Javascript frameworks (Angular.JS, React.JS etc) or plain HTML webpages.
PMA.live (now called PMA.collaboration) is a Javascript library that provided functionality to share your PMA.UI app/page with other users keeping all parts of the page synchronized, allowing for drawing annotations simultaneously, share multiple viewports just like with multi-head microscopes but over the internet.
We will create an example React application and integrate both PMA.UI and PMA.live as an example. You can use an existing React application or create a new one using create-react-app e.x.
npx create-react-app CollaborationReact
First we have to install PMA.UI and PMA.live library using npm by running
npm i @pathomation/pma.ui npm i @pathomation/pma.collaboration
inside application’s directory.
Next step is to add JQuery in our app. Open index.html
file inside public directory and add
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
in the head tag.
It’s time to implement the main functionality of PMA.UI and fire up 4 simultaneous slide viewports in the same page. We will use the PMA.UI components to easily do that namely the context, slideloader and autologin. So go ahead and replace the default App.js with the following initialization page
const imageSet = ["Reference/Aperio/CMU-1.svs", "Reference/Aperio/CMU-2.svs", "Reference/3DHistech/CMU-1.mrxs", "Reference/3DHistech/CMU-2.mrxs"];
const createSlideLoader = (context, element) => {
return new SlideLoader(context, {
element: element,
overview: { collapsed: false },
dimensions: { collapsed: false },
scaleLine: true,
annotations: { visible: true, labels: true, showMeasurements: false },
digitalZoomLevels: 2,
loadingBar: true,
highQuality: true,
filename: true,
barcode: false
});
}
function App() {
const viewerContainer = useRef(null);
const slideLoaderRefs = useRef([]);
const context = useRef(new UIContext({ caller: "Vas APP" }));
const [slideLoaders, setSlideLoaders] = useState([]);
const location = useLocation();
useEffect(() => {
if (slideLoaderRefs.current.length == 0) {
slideLoaderRefs.current = [...Array(20)].map((r, i) => slideLoaderRefs[i] || createRef());
}
let autoLogin = new AutoLogin(context.current, [{ serverUrl: pmaCoreUrl, username: username, password: password }]);
}, []);
useEffect(() => {
if (slideLoaderRefs.current.filter(c => !c || !c.current).length > 0) {
return;
}
let slLoaders = [];
for (let i = 0; i < slideLoaderRefs.current.length; i++) {
slLoaders.push(createSlideLoader(context.current, slideLoaderRefs.current[i].current));
}
setSlideLoaders(slLoaders);
}, [slideLoaderRefs.current]);
return (
<div className="App">
<div ref={viewerContainer} className="flex-container">
{slideLoaderRefs.current && slideLoaderRefs.current.map((r, i) =>
<div className={"flex-item"} key={i} ref={slideLoaderRefs.current[i]}></div>)
}
</div>
</div >
);
}
export default App;
To properly show all 4 viewers in the same page we need some css to style it up, so we need to add this to index.css. This will split the page to a grid of 2×2 viewers using css flex.
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
height: 850px;
}
.flex-item.pma-ui-viewport-container {
flex: 0 0 50%;
height: 400px
}
.ml-1 {
margin-left: 15px
Well that was easy to set up!
Collaboration
So let’s synchronize this page for all the user’s joining so they can see and interact with the same slides. For this we will be using the PMA.live server and to pma.collaboration package we installed earlier. To enable users to collaborate they have to join the same session, as it is called, one user will be the master of the session which controls the current viewports, slides and annotations(even though this can be changed with a setting for all users with a setting called EveryoneInControl).
PMA.live uses SignalR and the WebSocket protocol to achieve real time communication between participants, so we need to include this scripts in our page. We can include this scripts in the index.html as we did for jQuery, but we need to be sure that the scripts are properly loaded before trying to initialize any PMA.collaboration in our React application. So we will use the same trick used by Google Maps to load the scripts asynchronously and notify our React app when they are ready. So create a new file called collaborationHelpers.js with the following function.
export const loadSignalRHubs = (collaborationUrl, scriptId, callback) => {
const existingScript = document.getElementById(scriptId);
if (!existingScript) {
const script = document.createElement('script');
script.src = `${collaborationUrl}bundles/signalr`;
script.id = scriptId;
document.body.appendChild(script);
script.onload = () => {
const script2 = document.createElement('script');
script2.src = `${collaborationUrl}signalr/hubs`;
script2.id = scriptId + "hubs";
document.body.appendChild(script2);
script2.onload = () => {
if (callback) {
callback();
}
};
};
return;
}
if (existingScript && callback) {
callback()
};
};
To notify our React app that the scripts are ready and to proceed with the initialization we need to create a new state in the App.js page called loadedScripts which we will set to true when the scripts are loaded in our previous useEffect function
useEffect(() => {
if (slideLoaderRefs.current.length == 0) {
slideLoaderRefs.current = [...Array(20)].map((r, i) => slideLoaderRefs[i] || createRef());
}
let autoLogin = new AutoLogin(context.current, [{ serverUrl: pmaCoreUrl, username: "zuidemo", password: "zuidemo" }]);
loadSignalRHubs(collaborationUrl, "collaboration", () => {
setLoadedScripts(true);
});
}, []);
So now everything is ready to establish a connection to the PMA.live backend, and also joining a session (joining a non-existing session will just create a new one)
const initCollaboration = (nickname, isMaster, getSlideLoader, collaborationDataChanged, chatCallback) => {
return Collaboration.initialize(
{
pmaCoreUrl: pmaCoreUrl,
apiUrl: collaborationUrl + "api/",
hubUrl: collaborationUrl + "signalr",
master: isMaster,
getSlideLoader: getSlideLoader,
dataChanged: collaborationDataChanged,
owner: "Demo",
pointerImageSrc: "…",
masterPointerImageSrc: "…"
}, []).
then(function () {
var sessionName = "DemoSession";
var sessionActive = false;
var everyoneInControl = false;
return Collaboration.joinSession(sessionName, sessionActive, nickname, everyoneInControl);
}).
then(function (session) {
// after join session
////var appData = Collaboration.getApplicationData();
if (isMaster) {
Collaboration.setApplicationData({ a: 1 });
}
});
}
The initialize method tells to the PMA.live Collaboration static object where to find PMA.core, PMA.live backend, whether or not the current user is the session owner, what icons to show for the users’ and the master’s cursor and accepts a couple of callback functions. The joinSession method will create a session if it does not exist and then join it. If the session doesn’t exist, it is possible to specify whether or not it is currently active and if all users can take control of the synced viewports or only if the session owner can do this. Once the session has been created, only the session owner can modify it and change it’s active status or give control to others.
In order for PMA.live to be able to sync slides, it has to know the viewports it should work with. Earlier in our code we created an array of maximum of 20 slideloaders which we kept in a react ref object. Now let’s go back to the implementation of the “getSlideLoader” callback that we used during the initialization of PMA.live. This function will be called by PMA.live when it needs to attach to a viewport in order to control it. So we will need to return the appropriate slideLoader from this React Ref array with this function
const getSlideLoaderCb = (index, totalNumberOfImages) => {
if (!master && totalNumberOfImages < numberOfImages) {
for (let i = totalNumberOfImages; i < numberOfImages; i++) {
slideLoaders[i].load(null);
}
setNumberOfImages(totalNumberOfImages);
}
return slideLoaders[index];
}
So now we can initialize the collaboration in a useEffect which executes after the SignalR and Hubs scripts are properly initialized
useEffect(() => {
if (loadedScripts && viewerContainer.current && slideLoaderRefs.current && slideLoaders.length > 0) {
if (slideLoaderRefs.current.filter(c => !c || !c.current).length > 0) {
return;
}
if (collaborationInit) {
return;
}
initCollaboration("demo user", master, (index, totalNumberOfImages) => {
return getSlideLoaderCb(index, totalNumberOfImages);
},
() => {
let data = Collaboration.getApplicationData();
let session = Collaboration.getCurrentSession();
setCollaborationData({ data: data, session: session });
})
.then(() => {
setCollaborationInit(true);
});
}
}, [loadedScripts, viewerContainer, master, slideLoaders.length, collaborationInit, slideLoaderRefs, slideLoaderRefs.current.length]);
Finally, let’s talk about the “collaborationDataChanged” callback. Besides the out of the box slide syncing capabilities, it gives you the ability to exchange data between the users, in real time. This could be useful for example if you wanted to implement a chat system on top of the session. Every user can change the session’s data by invoking the Collaboration.setApplicationData method. It accepts a single object that will be shared among users. Whenever this method is called, all other users will receive it through the collaborationDataChanged callback. To do this is a React way we simply set the application data to a React state object whenever the callback is called.
To allow other users to join our application as guests we will implement a query string parameter called master. When this parameter is set to false users joining this session will be guests. We keep this value in a React state called master. So we change our initial useEffect function to add this
var urlSP = new URLSearchParams(location.search);
if (urlSP.get("master") === "false") {
setMaster(false);
}
Congratulations you’ve done it! You can now have a working React application with PMA.UI and collaboration featured enabled.
You can download a complete demo here
Additional developer resources are provided here