No but like the code will be in the cloud function can I use localhost because I think that if I use the server's url then it would be slow in terms of performance
Sorry if I am not making sense but I believe it will affect the performance
You're
Let me try to make things more clearer
There few ways around it
I have made a cloud function for appwrite:
import java.util.Map;
import java.util.HashMap;
public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception {
Map response = new HashMap();
response.put("message", "Hello from Appwrite!");
return res.json(response);
}```
and I have built it.
Now I want this function to modify one of the collections inside a database.
As per you we need to use the Server SDK for it. But I need to initialize the client with an endpoint. Does using the server's url which is abc.com (as an example) result in slower performance instead of localhost? And can we even use localhost in this case?
Btw, is this cloud or self-hosted?
Self hosted
👍
I hope that you understood my goal here?
Yes
You can't use localhost as it will be the function container and not your Appwrite
What you can do
- Use the server internal IP. For example, in my server it's
172.19.0.1
so you can insert as the endpoint. you just need to make sure it's not going to change. So you can initliaze the client like so:
client
.setEndpoint("http://172.19.0.1/v1")
.setProject(variables.get("APPWRITE_FUNCTION_PROJECT_ID"))
.setKey(variables.get("APPWRITE_FUNCTION_API_KEY"));
- Or, you can attach (not much recommend) the main container to the
runtimes
network by adding this to theyml
file like so
appwrite:
image: appwrite/appwrite:1.3.8
container_name: appwrite
<<: *x-logging
restart: unless-stopped
networks:
- appwrite
- runtimes
labels:
....
Then run docker compose down && docker compose up -d
and you'll be able to intiliaze the client using the container address like so:
client
.setEndpoint("http://appwrite/v1")
.setProject(variables.get("APPWRITE_FUNCTION_PROJECT_ID"))
.setKey(variables.get("APPWRITE_FUNCTION_API_KEY"));
Okay so I will use the internal IP but will it be efficient in terms of performance?
How many requests you're expecting in any given second?
I believe 4-5 currently but it may increase upto 100-120 later on
And what are the specs of that server?
2 vCores and 4 GB RAM
It should help, but I don't think that much performance wise
Then what should be the recommended specs?
It's not the recommended specs as the overall use of the appwrite
containers.
This server is great, but when it come to many functions execution per-second in the server you may experience this
https://github.com/appwrite/appwrite/issues/5629
You can try the semi-solution which will cover both the performances aspect and the local connection
Thank you. It is helpful. I will follow up in case I do need to scale up the power. My next question is that the cloud functions have python, ruby, nodejs and java as the default runtime. The server sdk isnt offered in java but does have kotlin. Can we use that for creating java functions without any bugs ?
Yes, it should work out of the box
It's recommend you'll create your function using the appwrite init function
cli command
Also, you can add more runtimes by editing the _APP_FUNCTIONS_RUNTIMES
variable.
https://appwrite.io/docs/environment-variables#functions
Also in this sample code:
import java.util.Map;
import java.util.HashMap;
public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception {
Map response = new HashMap();
response.put("message", "Hello from Appwrite!");
return res.json(response);
}```
There's a RuntimeResponse object. I didnt find any documentation for it nor RuntimeRequest.
They're small helpers You can take a look of them here https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/java-18.0/src/main/java/io/openruntimes/java
Ah I see. Thank you for all your help 🙂
Recommended threads
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...
- Send Email Verification With REST
I am using REST to create a user on the server side after receiving form data from the client. After the account is successfully created i wanted to send the v...
- Use different email hosts for different ...
Hello, I have 2 projects and i want to be able to set up email templates in the projects. Both projects will have different email host configurations. I see ...