-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get remote service function (#657)
* Add get remote service function * Fix auto create workspace * Fix conftest
- Loading branch information
Showing
11 changed files
with
60 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,25 +202,46 @@ 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://<server_url>/<workspace>/services/<client_id>:<service_id> (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 | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].24/dist/hypha-rpc-websocket.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].26/dist/hypha-rpc-websocket.min.js"></script> | ||
``` | ||
|
||
Use the following code in JavaScript to connect to the server and access an existing service: | ||
|
||
```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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].24/dist/hypha-rpc-websocket.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].26/dist/hypha-rpc-websocket.min.js"></script> | ||
``` | ||
|
||
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 | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].24/dist/hypha-rpc-websocket.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].26/dist/hypha-rpc-websocket.min.js"></script> | ||
<script> | ||
async function main(){ | ||
const server = await hyphaWebsocketClient.connectToServer({"server_url": "https://hypha.amun.ai"}); | ||
|
@@ -197,7 +197,7 @@ We created a tutorial to introduce this new feature: [service type annotation](. | |
Here is a quick example in JavaScript: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].24/dist/hypha-rpc-websocket.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].26/dist/hypha-rpc-websocket.min.js"></script> | ||
|
||
<script> | ||
async function main(){ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,7 +134,7 @@ if __name__ == "__main__": | |
**JavaScript Client: Service Usage** | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].24/dist/hypha-rpc-websocket.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].26/dist/hypha-rpc-websocket.min.js"></script> | ||
<script> | ||
async function main() { | ||
const server = await hyphaWebsocketClient.connectToServer({"server_url": "https://hypha.amun.ai"}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"version": "0.20.24" | ||
"version": "0.20.26" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters