title: How to implement express-ws route
body: <p>UPDATE:</p>
  
  <p>I needed to set up express-ws properly:</p>
  
  <pre><code>const express = require('express');
  let expressWs = require('express-ws');
  expressWs = expressWs(express());
  const app = expressWs.app;
  const router = express.Router();
  
  const createError = require('http-errors');
  const path = require('path');
  const cookieParser = require('cookie-parser');
  const logger = require('morgan');
  ...
  const playerHeadingRouter = require('./routes/playerHeading')(router, db);
  </code></pre>
  
  <p>along with my route module:</p>
  
  <pre><code>module.exports = (router, db) =&gt; {
    router.get('/api/player/:id', (req, res, next) =&gt; {
      res.end();
    });
  
    router.ws('/api/player/:id', (ws, req) =&gt; {
      ws.on('message', function(msg) {
        const coords = JSON.parse(msg);
        db.any(`UPDATE "user" SET x = ${coords.x} WHERE id = ${req.params.id}`)
        db.any(`UPDATE "user" SET y = ${coords.y} WHERE id = ${req.params.id}`)
        db.any(`UPDATE "user" SET geom = ST_SetSRID(ST_MakePoint(x, y),27700) WHERE id = ${req.params.id}`)
          .then(() =&gt; {
            // success;
            console.log('msg', msg);
          })
          .catch(error =&gt; {
            // error;
            console.log('error', error);
          });
      });
      // socket
    });
  }
  </code></pre>
  
  <p>now, when I use chrome websockets client, I can establish a connection to:</p>
  
  <pre><code>ws://localhost:3001/api/player-heading/2
  </code></pre>
  
  <p>and send a msg that gets used in a DB query from my express server.</p>