Hello, I’ve got a nice netlify nodejs BE running at ‘airbaronbe’ (e.g. https://airbaronbe.netlify.app/api/v will print the version number). Works fine.
BUT, I also have the server open up a second port, at 3002, for web sockets:
let port = process.env.SOCKET_PORT;
console.log(port = ${port});
const wsS = new WebSocketServer({ port, clientTracking: true });
I _do have SOCKET_PORT defined in my site’s environment variables.
When I run this locally, all’s good, I can ping localhost:3002/api/ from an FE app and connect, or I can even type ‘localhost:3002/api/v’ into a browser and get ‘Upgrade Required’, which is correct.
Similarly, if I try to connect from the FE programmatically, it errors with “Failed to execute ‘send’ on ‘WebSocket’: Still in CONNECTING state.”
what is the correct URL I should be using here, or is there more I need to do to get websockets working? I looked on the forum and didn’t see anything < 4 years old on this
Thanks Nathan, I should be clearer, and as you can see i’m no expert in this.
I am running a nodeJS web server (by which I mean that due to the magic of netlify, http://airbaronbe.netlify.app responds to http GET/POST/PUT requests, the response being computed by the deployed nodejs code), connected to an external mongoDB database. That part all runs fine.
It’s adding a websocket server on a different port that isn’t working. So I’m confused by the pointer to the doc that says " With a Jamstack site, there is no application server or language runtime (meaning no node.js…") when I do have a netlify site backed by node.js to respond to HTTP calls up and running.
@kfishkin Do you have a public repository for your code?
“Server side” functionality on Netlify is achieved with serverless functions / edge functions.
These can respond to requests like a traditional server, but they’re not persistently running.
Since using serverless functions “isn’t the same thing”, it’s not configured in same way, nor does it necessarily have feature parity with another way of doing things.
This is a very old thread now (so things may be different), but it sums it up pretty succinctly:
Thanks for the super-quick response Nathan, the repo is at https://github.com/kfishkin/ab_be.
It’s a private repo though, happy to add you if you give me your github account name.
the web socket connection is created in src/MessageGateway.js, starting at line 25. That code is itself called from routing.js (line 13), and THAT is invoked from index.js (line 6) (when running locally), and from line 12 in api.js when running under Netlify.
No worries I completely understand it being private, but I’ll pass.
I only answer questions here for two reasons:
To help developers get themselves ‘unstuck’ faster
To help developers that stumble upon the thread in future and can use the info to self-solve
I understand why some things are private, I’ve no issue with it myself, but helping with anything private is unfortunately just free consultancy and I have my actual work to do.
I’ve already provided enough pointers for you to keep researching to determine if what you’re trying to do is actually achievable on Netlify.
My guess is that it isn’t, and that you’d need to look in to another way of performing it.
For example using another provider or hosted service for the websocket functionality.