Skip to content

Commit 4bf4877

Browse files
Reporting to Socket.IO Admin UI (#1164)
1 parent c85c7d8 commit 4bf4877

23 files changed

+1810
-102
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
exclude:
2727
# pypy3 currently fails to run on Windows
2828
- os: windows-latest
29-
python: pypy-3.8
29+
python: pypy-3.9
3030
fail-fast: false
3131
runs-on: ${{ matrix.os }}
3232
steps:

docs/server.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,41 @@ callbacks when emitting. When the external process needs to receive callbacks,
617617
using a client to connect to the server with read and write support is a better
618618
option than a write-only client manager.
619619

620+
Monitoring and Administration
621+
-----------------------------
622+
623+
The Socket.IO server can be configured to accept connections from the official
624+
`Socket.IO Admin UI <https://socket.io/docs/v4/admin-ui/>`_. This tool provides
625+
real-time information about currently connected clients, rooms in use and
626+
events being emitted. It also allows an administrator to manually emit events,
627+
change room assignments and disconnect clients. The hosted version of this tool
628+
is available at `https://admin.socket.io <https://admin.socket.io>`_.
629+
630+
Given that enabling this feature can affect the performance of the server, it
631+
is disabled by default. To enable it, call the
632+
:func:`instrument() <socketio.Server.instrument>` method. For example::
633+
634+
import os
635+
import socketio
636+
637+
sio = socketio.Server(cors_allowed_origins=[
638+
'http://localhost:5000',
639+
'https://admin.socket.io',
640+
])
641+
sio.instrument(auth={
642+
'username': 'admin',
643+
'password': os.environ['ADMIN_PASSWORD'],
644+
})
645+
646+
This configures the server to accept connections from the hosted Admin UI
647+
client. Administrators can then open https://admin.socket.io in their web
648+
browsers and log in with username ``admin`` and the password given by the
649+
``ADMIN_PASSWORD`` environment variable. To ensure the Admin UI front end is
650+
allowed to connect, CORS is also configured.
651+
652+
Consult the reference documentation to learn about additional configuration
653+
options that are available.
654+
620655
Debugging and Troubleshooting
621656
-----------------------------
622657

examples/server/asgi/app.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
#!/usr/bin/env python
2-
import uvicorn
32

3+
# set instrument to `True` to accept connections from the official Socket.IO
4+
# Admin UI hosted at https://admin.socket.io
5+
instrument = False
6+
admin_login = {
7+
'username': 'admin',
8+
'password': 'python', # change this to a strong secret for production use!
9+
}
10+
11+
import uvicorn
412
import socketio
513

6-
sio = socketio.AsyncServer(async_mode='asgi')
14+
sio = socketio.AsyncServer(
15+
async_mode='asgi',
16+
cors_allowed_origins=None if not instrument else [
17+
'http://localhost:5000',
18+
'https://admin.socket.io', # edit the allowed origins if necessary
19+
])
20+
if instrument:
21+
sio.instrument(auth=admin_login)
22+
723
app = socketio.ASGIApp(sio, static_files={
824
'/': 'app.html',
925
})

examples/server/wsgi/app.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
# installed
44
async_mode = None
55

6+
# set instrument to `True` to accept connections from the official Socket.IO
7+
# Admin UI hosted at https://admin.socket.io
8+
instrument = False
9+
admin_login = {
10+
'username': 'admin',
11+
'password': 'python', # change this to a strong secret for production use!
12+
}
13+
614
from flask import Flask, render_template
715
import socketio
816

9-
sio = socketio.Server(logger=True, async_mode=async_mode)
17+
sio = socketio.Server(
18+
async_mode=async_mode,
19+
cors_allowed_origins=None if not instrument else [
20+
'http://localhost:5000',
21+
'https://admin.socket.io', # edit the allowed origins if necessary
22+
])
23+
if instrument:
24+
sio.instrument(auth=admin_login)
25+
1026
app = Flask(__name__)
1127
app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app)
1228
app.config['SECRET_KEY'] = 'secret!'

examples/server/wsgi/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.2/socket.io.min.js"></script>
77
<script type="text/javascript" charset="utf-8">
88
$(document).ready(function(){
9-
var socket = io.connect({transports: ['websocket']});
9+
var socket = io.connect();
1010

1111
socket.on('connect', function() {
1212
socket.emit('my_event', {data: 'I\'m connected!'});

0 commit comments

Comments
 (0)