diff --git a/docs/getting-started.md b/docs/getting-started.md index c81aebf1..b8b75173 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -85,7 +85,7 @@ async def start_server(server_url): print("Hello " + name) return "Hello " + name - await server.register_service({ + svc = await server.register_service({ "name": "Hello World", "id": "hello-world", "config": { @@ -94,8 +94,11 @@ async def start_server(server_url): "hello": hello }) - print(f"Hello world service registered at workspace: {server.config.workspace}") - print(f"Test it with the HTTP proxy: {server_url}/{server.config.workspace}/services/hello-world/hello?name=John") + print(f"Hello world service registered at workspace: {server.config.workspace}, id: {svc.id}") + + print(f'You can use this service using the service id: {svc.id}') + + print(f"You can also test the service via the HTTP proxy: {server_url}/{server.config.workspace}/services/{svc.id}/hello?name=John") # Keep the server running await server.serve() @@ -117,7 +120,7 @@ def start_server(server_url): print("Hello " + name) return "Hello " + name - server.register_service({ + svc = server.register_service({ "name": "Hello World", "id": "hello-world", "config": { @@ -126,8 +129,11 @@ def start_server(server_url): "hello": hello }) - print(f"Hello world service registered at workspace: {server.config.workspace}") - print(f"Test it with the HTTP proxy: {server_url}/{server.config.workspace}/services/hello-world/hello?name=John") + print(f"Hello world service registered at workspace: {server.config.workspace}, id: {svc.id}") + + print(f'You can use this service using the service id: {svc.id}') + + print(f"You can also test the service via the HTTP proxy: {server_url}/{server.config.workspace}/services/{svc.id}/hello?name=John") if __name__ == "__main__": server_url = "http://localhost:9527" @@ -166,8 +172,8 @@ async def main(): server = await connect_to_server({"server_url": "http://localhost:9527"}) # Get an existing service - # Since "hello-world" is registered as a public service, we can access it using only the name "hello-world" - svc = await server.get_service("hello-world") + # NOTE: You need to replace the service id with the actual id you obtained when registering the service + svc = await server.get_service("ws-user-scintillating-lawyer-94336986/YLNzuQvQHVqMAyDzmEpFgF:hello-world") ret = await svc.hello("John") print(ret) if __name__ == "__main__": @@ -184,8 +190,8 @@ def main(): server = connect_to_server({"server_url": "http://localhost:9527"}) # Get an existing service - # Since "hello-world" is registered as a public service, we can access it using only the name "hello-world" - svc = server.get_service("hello-world") + # NOTE: You need to replace the service id with the actual id you obtained when registering the service + svc = server.get_service("ws-user-scintillating-lawyer-94336986/YLNzuQvQHVqMAyDzmEpFgF:hello-world") ret = svc.hello("John") print(ret) @@ -196,12 +202,23 @@ if __name__ == "__main__": **NOTE: In Python, the recommended way to interact with the server to use asynchronous functions with `asyncio`. However, if you need to use synchronous functions, you can use `from hypha_rpc.sync import login, connect_to_server` instead. The have the exact same arguments as the asynchronous versions. For more information, see [Synchronous Wrapper](/hypha-rpc?id=synchronous-wrapper)** + +As a shortcut you can also use `get_remote_service`: +```python + +from hypha_rpc import get_remote_service + +# NOTE: You need to replace the service id with the actual id you obtained when registering the service +# The url format should be in the format: http:////services/: (client_id is optional) +svc = await get_remote_service("http://localhost:9527/ws-user-scintillating-lawyer-94336986/services/YLNzuQvQHVqMAyDzmEpFgF:hello-world") +``` + #### JavaScript Client Include the following script in your HTML file to load the `hypha-rpc` client: ```html - + ``` Use the following code in JavaScript to connect to the server and access an existing service: @@ -209,12 +226,22 @@ Use the following code in JavaScript to connect to the server and access an exis ```javascript async function main(){ const server = await hyphaWebsocketClient.connectToServer({"server_url": "http://localhost:9527"}) - const svc = await server.get_service("hello-world") + // NOTE: You need to replace the service id with the actual id you obtained when registering the service + const svc = await server.get_service("ws-user-scintillating-lawyer-94336986/YLNzuQvQHVqMAyDzmEpFgF:hello-world") const ret = await svc.hello("John") console.log(ret) } ``` +As a shortcut you can also use `hyphaWebsocketClient.getRemoteService`: + +```javascript +// NOTE: You need to replace the service id with the actual id you obtained when registering the service +const svc = await hyphaWebsocketClient.getRemoteService("http://localhost:9527/ws-user-scintillating-lawyer-94336986/services/YLNzuQvQHVqMAyDzmEpFgF:hello-world") +const ret = await svc.hello("John") +console.log(ret) +``` + ### Peer-to-Peer Connection via WebRTC By default all the clients connected to Hypha server communicate via the websocket connection or the HTTP proxy. This is suitable for most use cases which involves lightweight data exchange. However, if you need to transfer large data or perform real-time communication, you can use the WebRTC connection between clients. With hypha-rpc, you can easily create a WebRTC connection between two clients easily. See the [WebRTC support](/hypha-rpc?id=peer-to-peer-connection-via-webrtc) for more details. @@ -375,7 +402,7 @@ For more details, see the service request api endpoint [here](https://ai.imjoy.i Probes are useful for monitoring the status of services. They can be used to check whether a service is running correctly or to retrieve information about the service. Probes are executed by the server and can be used to monitor the health of services. -We create a convinient shortcut to register probes for monitoring services. Here is an example of how to register a probe for a service: +We create a convenient shortcut to register probes for monitoring services. Here is an example of how to register a probe for a service: ```python from hypha_rpc import connect_to_server diff --git a/docs/migration-guide.md b/docs/migration-guide.md index b566d925..27ffa911 100644 --- a/docs/migration-guide.md +++ b/docs/migration-guide.md @@ -15,7 +15,7 @@ To connect to the server, instead of installing the `imjoy-rpc` module, you will pip install -U hypha-rpc # new install ``` -We also changed our versioning strategy, we use the same version number for the server and client, so it's easier to match the client and server versions. For example, `hypha-rpc` version `0.20.24` is compatible with Hypha server version `0.20.24`. +We also changed our versioning strategy, we use the same version number for the server and client, so it's easier to match the client and server versions. For example, `hypha-rpc` version `0.20.26` is compatible with Hypha server version `0.20.26`. #### 2. Change the imports to use `hypha-rpc` @@ -128,10 +128,10 @@ loop.run_forever() To connect to the server, instead of using the `imjoy-rpc` module, you will need to use the `hypha-rpc` module. The `hypha-rpc` module is a standalone module that provides the RPC connection to the Hypha server. You can include it in your HTML using a script tag: ```html - + ``` -We also changed our versioning strategy, we use the same version number for the server and client, so it's easier to match the client and server versions. For example, `hypha-rpc` version `0.20.24` is compatible with Hypha server version `0.20.24`. +We also changed our versioning strategy, we use the same version number for the server and client, so it's easier to match the client and server versions. For example, `hypha-rpc` version `0.20.26` is compatible with Hypha server version `0.20.26`. #### 2. Change the connection method and use camelCase for service function names @@ -149,7 +149,7 @@ Here is a suggested list of search and replace operations to update your code: Here is an example of how the updated code might look: ```html - + + +