title: Socket.io async/await for .on()
body: <p>I'm building a socket.io Node JS application and my socket.io server will be listening for data from many socket.io clients, I need to save data to an API via my socket.io server as quickly as possible and figure that async/await is the best way forward.</p>
  <p>Right now, I've got a function inside my <code>.on('connection')</code>, but is there a way I can make this an async function rather than have a nested function inside?</p>
  <pre class="lang-js prettyprint-override"><code>io.use((socket, next)  =&gt; {
  
    if (!socket.handshake.query || !socket.handshake.query.token) {
      console.log('Authentication Error 1')
      return
    }
  
    jwt.verify(socket.handshake.query.token, process.env.AGENT_SIGNING_SECRET, (err, decoded) =&gt; {
      if (err) {
        console.log('Authentication Error 2')
        return
      }
      socket.decoded = decoded
      next()
    })
  
  }).on('connection', socket =&gt; {
    socket.on('agent-performance', data =&gt; {
      async function savePerformance () {
        const saved = await db.saveToDb('http://127.0.0.1:8000/api/profiler/save', data)
        console.log(saved)
      }
      savePerformance()
    })
  })
  </code></pre>