How to run the socket io server using gunicorn service

I'm using the socket.io service in my Django app, and I want to create one gunicorn service that is responsible for starting the socket.io service.

Below is the socket io server code

File name: server.py

from wsgi import application
from server_events import sio

app = socketio.WSGIApp(sio, application)


class Command(BaseCommand):
    help = 'Start the server'

    def handle(self, *args, **options):
        eventlet.wsgi.server(eventlet.listen(('127.0.0.1', 8001)), app)

Below is the actual code of the server with connect, disconnect and one custom method

File name: server_events.py

from wsgi import application

sio = socketio.Server(logger=True)
app = socketio.WSGIApp(sio, application)

@sio.event
def connected(sid, environ):
    print("Server connected with sid: {}".format(sid))

@sio.event
def disconnect(sid):
    print("Server disconnected with sid: {}".format(sid))

@sio.event
def run_bots(sid):
    print("func executed")
    **# Here custom logic**

When I hit python manage.py server in local, it will work fine, but on a production server, I don't want to type the python manage.py server command. What I want is to create one Gunicorn service and provide some instructions to that service so that when I hit the Gunicorn service command, it will start the socket IO server automatically, just like the runserver command.

I tried to implement those things by creating the Gunicorn service file, but it couldn't work.

socket-gunicorn.service

[Unit]
Description=SocketIO server
After=network.target

[Service]
Type=simple
User=ubuntu
Group=www-data

WorkingDirectory=/home/ubuntu/crypto-trading-bot

ExecStart=/home/ubuntu/crypto-trading-bot/venv/bin/python3 manage.py server >> /home/ubuntu/crypto-trading-bot/socketIO.log 2>&1
Restart=always


[Install]
WantedBy=multi-user.target

I am new to server-side stuff, so any help will be good.

Thanks

Back to Top