iTwin / itwinjs-core

Monorepo for iTwin.js Library
https://www.itwinjs.org
MIT License
601 stars 210 forks source link

Should IModelConnection.openSnapshot() require a connection to IModelHub? #50

Closed aderderian closed 4 years ago

aderderian commented 4 years ago

In the documentation and issues it says IModelConnection.openStandalone() is being removed in order to streamline operations through IModelConnection.openSnapshot(). The documentation however says we shouldn't use this in a web front-end. Can I use this? I am trying to "work offline" with the sample app in a web browser in order to try openSnapshot with a local file and I am getting an error stating I hav an invalid token for RPC operation request. I expected this would not need to talk to iModelHub since it's a snapshot. Thx

aderderian commented 4 years ago

Also looks related to https://github.com/imodeljs/imodeljs-samples/issues/6

scsewall commented 4 years ago

A snapshot iModel is a read-only file that is detached from and unknown to iModelHub. It therefore cannot be synchronized with iModelHub and does not have a change timeline. This is different from an iModel briefcase that is synchronized with iModelHub and does have a change timeline. A briefcase might not be able to be synchronized if there is not a network connection (offline), but it can when the network is available again. Anyway, a snapshot iModel is meant for archival and delivery workflows (conceptually similar to a PDF file).

Because you work with a snapshot iModel as a file, it is really only appropriate for desktop and mobile cases where the local backend has access to the file system. A web frontend does not have access to the web backend's file system as that would be a security issue.

aderderian commented 4 years ago

Understood on the first part. Based on the simple viewer example though, if I want to read a local file how would I do that? I want to open a snapshot file that was exported from another product. openSnapshot is requiring a token. In my scenario the backed could be on a web server, but it appears in the example the opening of the snapshot happens in the frontend. How do I make my backend work with a local file then?

ColinKerr commented 4 years ago

On the backend you can use the IModelDb.openSnapshot method.

API Docs: https://imodeljs.github.io/iModelJs-docs-output/reference/imodeljs-backend/imodels/imodeldb/opensnapshot/

Some description of snapshots: https://imodeljs.github.io/iModelJs-docs-output/learning/backend/accessingimodels/

The display-test-app in the iModel.js source is setup to open a snapshot iModel: https://github.com/imodeljs/imodeljs/tree/master/test-apps/display-test-app

Hopefully these links will help you implement something, let us know.

aderderian commented 4 years ago

@ColinKerr thanks so much. I did review some of these docs and it was not completely clear. It was clear that in a back-end you would use an IModelDb, where the front-end leverages IModelConnection. In the basic viewer app sample, it is not clear how that front-end goes through the back-end provided. Attempting to swap in the IModelDb piece to that was not super clear. If those links in your reply make that clear, then great. Thx!

ColinKerr commented 4 years ago

We have work to do to improve our documentation here. Your feedback on what has been helpful will help us update our docs in the future.

The key to opening it from the frontend is to use the IModelConnection.openSnapshot(fileName). For this to work the SnapshotIModelRpcInterface must be registered. See registration of this interface search for it in that display-test-app directory.

Checkout the resetIModel method in this file of the display-test-app: https://github.com/imodeljs/imodeljs/blob/master/test-apps/display-test-app/src/frontend/Viewer.ts

If you get and build the iModeljs git repo following the instructions in the readme you can then run the display-test-app yourself, that might help you follow the code.

This should all work with the simple-viewer-app but it does not at the moment. We will investigate. In the meantime please try the sample in the imodel.js repo

aderderian commented 4 years ago

Thanks, will do and report back!

aderderian commented 4 years ago

@ColinKerr , I had the chance to revisit this last night. When I run the display-test-app, I hit a couple of issues. First, for some reason when I run the app by leveraging the "npm run start:servers" command it seems to think I am in a mobile environment. It is not until I hard code some of the logic to force it to load from the configuration.json does it work. I open a browser in chrome to http://localhost:3000. Do you know how I can have it properly detect my browser environment? Secondarily, the configuration.json file lives in public and in lib/webresources. Which one is the one that is pulled in at run-time? The code comments say the public one, but it looks like that is not the case. What are mobile-configuration.json? config.json? In the end I got the model to load from the local location by hacking past the mobile piece, once I know that it will all make sense. Thx in n advance.

Also, I get an error upon reloading the browser if a model is already opened. Any reasoning for that?

pmconne commented 4 years ago

@aderderian I'll first paraphrase the disclaimer included in display-test-app's README: this app exists strictly for developer testing of display-related functionality. Its UI is primitive and unintuitive, and it uses undocumented, unstable, and potentially unsafe APIs. It is not intended to serve as an example of a "real" iModel.js-based app - that's what the samples are for.

That said, a handful of us developers who work on the display system use it every day and find it invaluable for testing; if you find it useful you should feel free to use it too.

Please confirm you are using up-to-date iModel.js packages. 1.7.0 contained a bug which caused the electron version of display-test-app to incorrectly report it was running on a mobile device. That was fixed in 1.8.0. https://github.com/imodeljs/imodeljs/blob/7afa5cbbd1301bc6bcb5970143a1d2f17bcf8fe0/core/common/src/rpc/mobile/MobileRpcManager.ts#L64

The /lib/ directory is where build output goes. During build configuration.json is copied from /public/ to /lib/webresources. We usually use environment variables to configure the app rather than editing the configuration file (see README).

If you reload the browser without closing the currently open iModel (by closing all viewports) then the iModel does not get closed.

aderderian commented 4 years ago

Hey @pmconne thanks so much for the tip on updating. I originally pulled the repo a couple of weeks ago, so yes it appears I was running 1.7. This all seems to work now for me. It sounds like if you reload the browser without closing the model first you may get into trouble when loading on startup from disk, the manual workaround seems to just restart the server. Also one strange thing I am seeing is that the contents of /public/configuration.json is not being copied into the lib/resources version upon running a build "rush build --to display-test-app". It does however get overwritten with default values when I start the server with "npm run start:servers". I then just have to manually update that file to get things to work.

I noticed the comment on environment variables, but am not familiar with using those in Typescript yet. I will take a look at how to set those. Thx again for your help!

ColinKerr commented 4 years ago

@pmconne is referring to the environment variables described here: https://github.com/imodeljs/imodeljs/blob/master/test-apps/display-test-app/README.md

How you set them depends on what environment you're working in. For example I'm running windows and have VS Code setup to use the bash shell so I set the default iModel to open in the shell like this:

export SVT_STANDALONE_FILENAME=/c/iModels/myiModel.i.bim

NOTE: my file has the windows path c:\iModels\myiModel.i.bim

Hope this helps you.

aderderian commented 4 years ago

Got it. Thanks! Yes, all is working well for me now.

wilmaier commented 4 years ago

FYI - I investigated the issues with the simple-viewer-app and found that recent changes caused this to break. I hope to have my fixes in a new version in the next couple days.

wilmaier commented 4 years ago

The issue with simple-viewer-app is now resolved in the latest version.

aderderian commented 4 years ago

Nice! Thanks, I will check it out.