old_contents: from sanic import Sanic
  from sanic.response import html
  
  import socketio
  
  sio = socketio.AsyncServer(async_mode='sanic')
  app = Sanic()
  sio.attach(app)
  
  
  async def background_task():
      """Example of how to send server generated events to clients."""
      count = 0
      while True:
          await sio.sleep(10)
          count += 1
          await sio.emit('my_response', {'data': 'Server generated event'})
  
  
  @app.listener('before_server_start')
  def before_server_start(sanic, loop):
      sio.start_background_task(background_task)
  
  
  @app.route('/')
  async def index(request):
      with open('app.html') as f:
          return html(f.read())
  
  
  @sio.event
  async def my_event(sid, message):
      await sio.emit('my_response', {'data': message['data']}, room=sid)
  
  
  @sio.event
  async def my_broadcast_event(sid, message):
      await sio.emit('my_response', {'data': message['data']})
  
  
  @sio.event
  async def join(sid, message):
      sio.enter_room(sid, message['room'])
      await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
                     room=sid)
  
  
  @sio.event
  async def leave(sid, message):
      sio.leave_room(sid, message['room'])
      await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
                     room=sid)
  
  
  @sio.event
  async def close_room(sid, message):
      await sio.emit('my_response',
                     {'data': 'Room ' + message['room'] + ' is closing.'},
                     room=message['room'])
      await sio.close_room(message['room'])
  
  
  @sio.event
  async def my_room_event(sid, message):
      await sio.emit('my_response', {'data': message['data']},
                     room=message['room'])
  
  
  @sio.event
  async def disconnect_request(sid):
      await sio.disconnect(sid)
  
  
  @sio.event
  async def connect(sid, environ):
      await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
  
  
  @sio.event
  def disconnect(sid):
      print('Client disconnected')
  
  
  app.static('/static', './static')
  
  
  if __name__ == '__main__':
      app.run()
  
new_contents: from sanic import Sanic
  from sanic.response import html
  
  import socketio
  
  sio = socketio.AsyncServer(async_mode='sanic')
  app = Sanic(name='sanic_application')
  sio.attach(app)
  
  
  async def background_task():
      """Example of how to send server generated events to clients."""
      count = 0
      while True:
          await sio.sleep(10)
          count += 1
          await sio.emit('my_response', {'data': 'Server generated event'})
  
  
  @app.listener('before_server_start')
  def before_server_start(sanic, loop):
      sio.start_background_task(background_task)
  
  
  @app.route('/')
  async def index(request):
      with open('app.html') as f:
          return html(f.read())
  
  
  @sio.event
  async def my_event(sid, message):
      await sio.emit('my_response', {'data': message['data']}, room=sid)
  
  
  @sio.event
  async def my_broadcast_event(sid, message):
      await sio.emit('my_response', {'data': message['data']})
  
  
  @sio.event
  async def join(sid, message):
      sio.enter_room(sid, message['room'])
      await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
                     room=sid)
  
  
  @sio.event
  async def leave(sid, message):
      sio.leave_room(sid, message['room'])
      await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
                     room=sid)
  
  
  @sio.event
  async def close_room(sid, message):
      await sio.emit('my_response',
                     {'data': 'Room ' + message['room'] + ' is closing.'},
                     room=message['room'])
      await sio.close_room(message['room'])
  
  
  @sio.event
  async def my_room_event(sid, message):
      await sio.emit('my_response', {'data': message['data']},
                     room=message['room'])
  
  
  @sio.event
  async def disconnect_request(sid):
      await sio.disconnect(sid)
  
  
  @sio.event
  async def connect(sid, environ):
      await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
  
  
  @sio.event
  def disconnect(sid):
      print('Client disconnected')
  
  
  app.static('/static', './static')
  
  
  if __name__ == '__main__':
      app.run()
  
subject: Add application name to Sanic example (#892)