Cloud function on local


#1

Hi
Thank you for the great product. I wanted to ask if can I use cloud function in local deployment.
If I can how to do that ??
Thanks a lot


#2

@KhaledAld Do you mean the JS cloud code?

See this example works for you? You can put your cloud functions in the same directory. skygear-node loads the index.js by default.


Example for Skygear Environment Variables
#3

Thanks @carmenlau
when i run dockerfile like this link :


i have this error :

plugin_1 | (node:6) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Cannot find module ‘/usr/src/app/index.js’
version_3_plugin_1 exited with code 0


#4

I will write installation steps:_
-First, I run Skygear server with chat plugins from docker-comose.yml file
-Then I install cloud function on local from (https://github.com/skygear-demo/cloud-chat-demo-js)
and add cloud config to docker file

PLUGINS: JS1
JS1_TRANSPORT: HTTP
JS1_PATH: http://192.168.1.34:9000

-Then I run Skygear server with (docker-compose up) and it works fine

-Finally, I run (/node_modules/.bin/skygear-node index.js )

and i have this message :

[info] plugin: Listening 0.0.0.0 on port 9000…, {“process”:“node”}

But cloud function is not working (don’t push send notification)

There is any mistake on my installation steps?

Note This is my docker file :

version: ‘2’

services:
db:
image: mdillon/postgis:9.5
ports:
- “5432:5432”
volumes:
- db_data:/var/lib/postgresql/data

app:
image: quay.io/skygeario/skygear-server:latest
ports:
- “3001:3000”
volumes:
- app_data:/go/src/app/data
links:
- db
command: skygear-server
environment:
DATABASE_URL: postgresql://postgres:@db/postgres?sslmode=disable
APP_NAME: testChat
API_KEY: _apiKey
MASTER_KEY: _masterkey
TOKEN_STORE: jwt
TOKEN_STORE_SECRET: jwt_secret
PLUGINS: CHAT , JS1
CHAT_TRANSPORT: zmq
CHAT_PATH: chat
CHAT_ARGS: tcp://0.0.0.0:5555
JS1_TRANSPORT: http
JS1_PATH: http://192.168.1.34:9000
ASSET_STORE: fs
ASSET_STORE_URL_PREFIX: http://localhost:3000/files
ASSET_STORE_SECRET: khaled_ald_gifted
APNS_ENABLE: “YES”
APNS_ENV: sandbox
APNS_TYPE: token
APNS_KEY_ID: xxxxxx
APNS_TEAM_ID: xxxxxxxxxx
APNS_TOKEN_KEY_PATH: /app/data/cert/auth.p8

plugin:
build:
context: .
links:
- app
- db
volumes:
- ./:/usr/src/app
command: py-skygear chat
environment:
DATABASE_URL: postgresql://postgres:@db:5432/postgres?sslmode=disable
PUBSUB_URL: ws://app:3000/pubsub
SKYGEAR_ADDRESS: tcp://app:5555
SKYGEAR_ENDPOINT: http://app:3000
APP_NAME: testChat
API_KEY: _apiKey
MASTER_KEY: _masterkey
TOKEN_STORE_SECRET: jwt_secret
ASSET_STORE_URL_PREFIX: http://localhost:3000/

volumes:
db_data:
driver: local
app_data:
driver: local


#5

To use this docker-compose, you need to put your cloud functions (index.js) in the same folder of the docker-compose.yaml. It runs index.js by default. Adding command /node_modules/.bin/skygear-node index.js to specify the cloud functions file also works.


#6

May I know if the chat plugin run correctly, only push notification doesn’t work?

For debugging, when the server start, you will see the logs that list out what functions are registered. If the js cloud function registered correctly, you should be able to see the lambda chat:after_message_sent_hook is registered.

Some suggestions for the setup. The JS plugin run outside the docker-compose, I think using http://192.168.1.34:9000 in the server container should have problem. If there is no strong reason, I would suggest putting the JS plugin into the same docker-compose.yaml. So you don’t have to connect it through 192.168.1.34


#7

Hi Mr @carmenlau

if I can’t add JS plugins inside docker-compose.yaml as plugins because I already have chat plugins and can’t add 2 plugins in docker file.

chat plugin is working fine.
push notification is working fine, but sending notification manually
I want to use cloud functions to make notification dynamically and to make RESTFull API

Thanks.


#8

PLUGINS: CHAT , JS1 . This is true??

CHAT_TRANSPORT: zmq
CHAT_PATH: chat
CHAT_ARGS: tcp://0.0.0.0:5555
JS1_TRANSPORT: http
JS1_PATH: http://192.168.1.34:9000

can i add two plugins in docker file ???


#9

Hi @KhaledAld, plugin is just a container, so you can have 2 plugins by giving the 2nd plugin another name. For example, for the js plungin

jsplugin:
    image: skygeario/skygear-node:v1.6.2
    .....

Then the JS1_PATH should become http://jsplugin:9000

PLUGINS: CHAT,JS1 is correct.


#10

Ok i will try it and tell you if work fine
Thanks for your time :heart_eyes:


#11

@carmenlau

when i build it , the server is shutdown and i have this error :
app_1 | goroutine 1 [running]:
app_1 | github.com/skygeario/skygear-server/pkg/server/plugin.NewPlugin(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc9e235, 0x5, 0xc420016089, …)
app_1 | /go/src/github.com/skygeario/skygear-server/pkg/server/plugin/plugin.go:107 +0x244
app_1 | github.com/skygeario/skygear-server/pkg/server/plugin.(*Context).AddPluginConfiguration(0xc42007fb00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10)
app_1 | /go/src/github.com/skygeario/skygear-server/pkg/server/plugin/plugin.go:131 +0xd4
app_1 | main.initPlugin(0xc9e235, 0x5, 0xc420016089, 0xa, 0xc42001a058, 0x5, 0xc4200160cb, 0xa, 0xc9dcc3, 0x4, …)
app_1 | /go/src/github.com/skygeario/skygear-server/main.go:662 +0x1cd
app_1 | main.main()
app_1 | /go/src/github.com/skygeario/skygear-server/main.go:401 +0x39a0
chatfinal_app_1 exited with code 2

and I have this Error when I set 2 PLUGINS: NODE , CHAT
If I run just one plugin everything works fine

  PLUGINS: NODE , CHAT
  TOKEN_STORE_SECRET: jwt_secret
  CHAT_TRANSPORT: zmq
  CHAT_PATH: chat
  CHAT_ARGS: tcp://0.0.0.0:5555
  NODE_TRANSPORT: http
  NODE_PATH: http://jsplugin:9000

#12

Please try removing the space between NODE and CHAT in PLUGINS? Something like this PLUGINS: NODE,CHAT.

Btw it would be better to separate the js and chat plugin in different folders, I think it is not the root cause of the problem but it would organize better.

I created a minimal working example for your reference. I put chat plugin codes in the ./chat folder and js code in ./js_plugin.

Hope this help! :slight_smile:


#13

It’s work fine :heart_eyes::heart_eyes:

Thanks a lot.