0

I have a very basic Express server that also has an HTTPS WebSocket server running on the same port. When I run this locally I'm able to hit the express endpoints and establish a connection between the WebSocket server and the client over https.

However, when I host this on IIS (Site is pointing to the correct path and bindings are established. WebSocket module is correctly configured. Works locally and deployed using http websockets). I can't establish the connection. I've read a few threads on this primarily HTTPS and iisnode and feel like my lack of understanding of how IIS handles https is the culprit here... I see that I'm establishing the server the same way as the original poster, but I'm struggling to see how I can establish the WebSocketServer without it.

index.js (relevant code only)

// Create express app.
var app = express();

// Open the logfile in append mode.
var logFile = fs.createWriteStream(LOG_PATH, { flags: 'a' });

const keyPath = fs.readFileSync(KEY_PATH, 'utf8');
const certPath = fs.readFileSync(CERT_PATH, 'utf8');
const credentials = { key: keyPath, cert: certPath };

// Create HTTPS server
const server = https.createServer(credentials, app);

// Attach WebSocket server to the HTTPS server
const wss = new WebSocketServer({ server, perMessageDeflate: false });

// Run the express app on the IIS Port.
server.listen(PORT, () => {
    logger('INFO', `Express Server Started, PORT: ${PORT}`);
});

wss.on('connection', (ws) => {
    logger('INFO', `Client has connected via Web Socket.`);

    ws.send('Hello from the server!');

    ws.on('message', function message(data) {
        logger('INFO', data.toString());
    });

    ws.on('close', () => {
        logger('INFO', `Client has disconnected.`);
    });
});

web.config

<configuration>
  <system.webServer>
    <handlers>
      <add name="ExpressServer" path="index.js" verb="*" modules="iisnode" resourceType="Unspecified" />
    </handlers>
    <webSocket enabled="true" />
    <rewrite>
      <rules>
        <rule name="sendToNode">
          <match url="/*" />
          <action type="Rewrite" url="index.js" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.