Can you connect two separate Heroku apps (frontend and backend) with a websocket connection?

Viewed 14

I'm not sure how typical of a structure this is, but I'm looking to connect two separate Heroku apps to each other using websockets, one being the React frontend and the other a Node app I have running on Heroku that does things like run a bot and automate some processes. I want to connect the two so that I can make requests that need to be server-side from the React frontend, e.g. sending emails with SendGrid. The server side will not necessarily even be serving webpages, they just need to communicate data between them. I am using socket.io, but would be open to just using ws.

I was getting CORS errors of course, so I tweaked the code, and then I was getting 503 errors, so I tweaked the code, and now the frontend console is just saying "websocket connection failed" without an error code.

Server:

const express = require('express');
const socket = require('socket.io');

const PORT = process.env.PORT || 3000;
const server = express()
    .listen(PORT, () => console.log(`Listening on ${PORT}`));
const io = socket(server, {
    cors: {
        origin: "https://my-client-app.herokuapp.com/",
        methods: ["GET", "POST"],
        credentials: true
    }
});

Client:

const io = require("socket.io-client");
var socket = io('https://my-server-app.herokuapp.com/', { transports : ['websocket', 'polling'] });

Error:

websocket.js:58 WebSocket connection to 'wss://my-server-app.herokuapp.com/socket.io/?EIO=4&transport=websocket' failed: 
doOpen  @   websocket.js:58
open    @   transport.js:57
open    @   socket.js:172
Socket  @   socket.js:110
open    @   manager.js:137
Manager @   manager.js:65
lookup  @   index.js:41
./src/App.js    @   App.js:12
options.factory @   react refresh:6
__webpack_require__ @   bootstrap:24
fn  @   hot module replacement:62
./src/index.js  @   App.js:1222
options.factory @   react refresh:6
__webpack_require__ @   bootstrap:24
(anonymous) @   startup:7
(anonymous) @   startup:7
0 Answers
Related