In this example we will create a simple Sockets. The idea here is to create an endpoint which will return
a Hello world message.
The whole example can be found under: Syncano/custom-socket-hello-world
It's possible to install it to Syncano Instance using install from url functionality in CLI. The URL is:
https://github.com/Syncano/custom-socket-hello-world/blob/master/socket.yml
- Syncano Account - Create one here.
- GIT - If you want to edit files locally, clone our repository using:
git clone git@github.com:Syncano/custom-socket-hello-world.git-
Syncano CLI tool in version 0.5 or higher.
Note: It is nice to use virtualenv to separate your tools:
sudo pip install virtualenvThen create virtual env:virtualenv cliand active it:source cli/bin/activateInstall Syncano CLI:pip install syncano_cli>=0.5
- Your favorite text editor.
name: hello_world
author:
name: Info
email: info@synano.com
description: Hello World example
endpoints:
hello_endpoint:
script: hello_world
dependencies:
scripts:
hello_world:
runtime_name: python_library_v5.0
file: scripts/hello_world.py
Above YAML file defines one Sockets with one endpoint:
hello_endpointfor printing hello world on every HTTP method call.
There is also one script dependency defined, to hello_world Script.
In my favorite editor the project look as follows:
The script (scripts/hello_world.py) consists of a few lines:
content = """
<!DOCTYPE html>
<html>
<head>
<title>Hello world!</title>
</head>
<body>
Hello World!
</body>
</html>
"""
set_response(HttpResponse(status_code=200, content=content, content_type='text/html'))
The above code executed in Syncano will return a valid HTML response with the Hello World! message inside.
The set_response is a function which returns a custom response (e.g. in JSON, CSV or HTML format) from the script.
As can be seen in the example above, the basic structure of this Sockets is:
.
├── scripts
│ └── hello_world.py
└── socket.yml
socket.yml file stores YAML definition of the Sockets, and under scripts directory there is a definition
of Sockets dependencies (currently of type script).
-
Assuming that you have Syncano CLI installed, log in using:
syncano login --instance-name your-instance-nameIn my case it is:syncano login --instance-name patient-resonance-4283Next you will see a prompt for
usernameandpassword; provide both and confirm withenter. -
There are two ways of installing a Sockets - one is using your local files and the second one is by using a URL.
To install the Sockets from url do:
syncano sockets install https://raw.githubusercontent.com/Syncano/custom-socket-hello-world/master/socket.yml --name hello_worldIn this scenario - you do not even need to clone the repository to your local machine. The
--nameparameter and name here are needed - because under the hood, an empty Sockets is created - and fetching code from the repository is done asynchronously in the second step.To install Sockets from local files do:
syncano sockets install <path_to_files>In my case it is:
syncano sockets install ../syncano_scripts/repos/custom-socket-hello-world/So you need to point to the parent directory of your
socket.ymldefinition. -
Try a newly created Sockets:
To list Sockets, do:
syncano sockets listIn the output you should find:
- socket: info: '' name: hello_world status: okThis means that Sockets
hello_worldwas created successfuly - the status isok. In any other case you will see here anerrorand detailed information ininfoabout what went wrong.Now, list all defined endpoints:
syncano sockets list endpointsIn the output you should find:
- endpoint: methods: - POST - PUT - PATCH - GET - DELETE name: hello_world/hello_endpoint path: /v1.1/instances/your-instance-name/endpoints/sockets/hello_world/hello_endpoint/ -
Run the endpoint defined in Sockets:
syncano sockets run hello_world/hello_endpointYou should see in the output raw html file:
<!DOCTYPE html> <html> <head> <title>Hello world!</title> </head> <body> Hello World! </body> </html>Lets see if output can be seen in the browser: Go to:
https://api.syncano.io/v1.1/instances/your-instance-name/endpoints/sockets/hello_world/hello_endpoint/?api_key=your_api_keyWe defined the endpoint to handle GET HTTP method.In my case:
-
To delete Sockets do:
syncano sockets delete hello_world
Hope this was helpful! If you have any question or issues, do no hesitate to contact me directly: sebastian.opalczynski@syncano.com I am also available on the Syncano Slack community. See you there!

