Service Types
Backend Service Type Selection
This article provides a detailed introduction to the two communication types for GMSSH application backend services, helping developers choose the appropriate service architecture based on application requirements.
Service Type Overview
The GMSSH platform supports two backend service communication types:
| Type | Features | Use Cases | Performance |
|---|---|---|---|
| Socket | Unix Domain Socket communication | High-performance apps, frequent communication | High |
| HTTP | Standard HTTP protocol | Standard web apps, third-party integration | Medium |
Recommended Choice
We recommend using the Socket type because the GMSSH SDK is based on Socket, providing better performance and development experience.
Socket Service Type
Features and Advantages
- High Performance: Based on Unix Domain Socket, more efficient inter-process communication
- Low Latency: Local file socket communication, no network overhead
- SDK Support: Official SDK is fully based on Socket
- Security: Local communication, avoiding network security risks
Configuration Requirements
When deploying the application, Socket services must create socket files at the following location:
/.__gmssh/plugin/{org-name}/{app-name}/tmp/app.sockImplementation Example
Create a Socket service using the Python SDK:
# main.py
import asyncio
from simplejrpc.app import ServerApplication
from simplejrpc.response import jsonify
# Create application instance, specify socket file path
app = ServerApplication("app.socket")
@app.route(name="ping")
async def ping():
"""Health check"""
return jsonify(code=200, data="pong", msg="OK")
@app.route(name="hello")
async def hello():
"""Simple greeting endpoint"""
return jsonify(code=200, data="Hello, GMSSH!", msg="Request successful")
if __name__ == "__main__":
asyncio.run(app.run())SDK Documentation
For detailed Socket service development guide, please refer to Python SDK Documentation.
HTTP Service Type
Features and Advantages
- Standard Protocol: Uses standard HTTP protocol, good compatibility
- Easy to Debug: Can use standard HTTP tools for testing
- Third-Party Integration: Easy to integrate with existing HTTP services
- Cross-Platform: Does not depend on specific operating system features
Configuration Requirements
HTTP services need to write the port number to a specified file at startup:
/.__gmssh/plugin/{org-name}/{app-name}/tmp/app.portPort Management
Port Requirements
- Must use random ports to avoid conflicts
- Need to check port availability before startup
- Write port number to app.port file
Implementation Example
Create an HTTP service using Flask:
import socket
import os
from flask import Flask, jsonify
app = Flask(__name__)
def get_free_port():
"""Get an available random port"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 0))
s.listen(1)
port = s.getsockname()[1]
return port
def write_port_file(port):
"""Write port number to file"""
# todo: In actual applications, generate path dynamically based on org and app name
port_file = '/.__gmssh/plugin/{org-name}/{app-name}/tmp/app.port'
os.makedirs(os.path.dirname(port_file), exist_ok=True)
with open(port_file, 'w') as f:
f.write(str(port))
@app.post("/hello")
def hello():
return jsonify(code=200, data="hello world!", msg="OK")
@app.get("/ping")
def ping():
return jsonify(code=200, data="pong", msg="OK")
if __name__ == '__main__':
port = get_free_port()
write_port_file(port)
app.run(host='127.0.0.1', port=port)Selection Recommendations
Recommend Using Socket
Suitable for the following scenarios:
- Newly developed GMSSH applications
- Applications requiring high-performance communication
- Applications developed using official SDK
- Real-time applications sensitive to latency
Consider Using HTTP
Suitable for the following scenarios:
- Need to integrate with existing HTTP services
- Developed using third-party frameworks
- Applications requiring standard REST APIs
- Applications with high cross-platform compatibility requirements
Development Recommendations
- Prioritize Socket: Unless there are special requirements, we recommend using the Socket type
- Use Official SDK: Can greatly simplify the development process
- Follow Path Specifications: Strictly create socket files or port files as required
- Handle Errors Well: Ensure appropriate error prompts when service startup fails
