49

I am new to WebRTC and WebSockets and was following this tutorial to create a WebRTC demo project, but I am unable to create a WebSocket connection. I have followed the same steps as mentioned in the project. His project is running on port 8080 and he mentioned ws://localhost:9090. My project is running on port 8081, but I copied his URL ws://localhost:9090 because I didn't know the significance of 9090 and I received this error and my server is node.js. i changed local host to 8081 as well but then i am getting hand shake error.

WebSocket connection to 'ws://localhost:9090/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Hafsa
  • 521
  • 1
  • 5
  • 6
  • 2
    Think of a port as a door. You (the client) need an open door in the house (server) in order to enter. So if your server listens on port 8081, you (the client) must connect through port 8081. Paste your whole code so you can get more help. – sidewinder Jun 15 '17 at 20:03
  • https://www.tutorialspoint.com/webrtc/webrtc_rtcpeerconnection_apis.htm i am following these instruction to create a simple web-application @sidewinder if i change my local host to 8081 then i get this error : WebSocket connection to 'ws://localhost:8081/' failed: Connection closed before receiving a handshake response – Hafsa Jun 15 '17 at 20:06
  • can u give me the better tutorials link ? @sidewinder i found this tutorial very simple as i am a beginner – Hafsa Jun 15 '17 at 20:11
  • If you can give me some better links for webRTC tutorial to develop an application, i will be glad @sidewinder – Hafsa Jun 15 '17 at 20:15
  • It could also happen if you put your browser in offline mode (in service worker settings) and then you lose your **websocket connection to the vscode's live server**, or something like that. – aderchox Nov 12 '22 at 07:08

8 Answers8

41

Chrome doesn't allow unsecure websocket (ws) connections to localhost (only wss, so you should setup a TLS certificate for your local web/websocket server). However the same should work fine with Firefox.

Istvan
  • 1,591
  • 1
  • 13
  • 19
  • 3
    I tried to run websocket with the url var wsUri = "ws://localhost:55195/" and it says WebSocket connection to 'ws://localhost:55195/' failed: Have been looking to fix this issue since 4 days, still not getting what is wrong with the code? any suggestion to fix this problem will be highly appreciated. let me know if anyone need to see the code as well! thanks in advance. – Shantilal Suthar Jun 28 '21 at 05:10
  • its right in the answer you commented into. add a certificate to your ws server and connect via wss:// for it to work in localhost – Ali Mert Çakar May 18 '23 at 19:14
13

You need to use ws://yourIp:9090/, where yourIP is like 192.168.?.?.

double-beep
  • 5,031
  • 17
  • 33
  • 41
4

Usually WebRTC requires a secure connection (that is https). The error you have got is due to TLS/SSL certificates occupied, may be they are not properly configured in your project. Provide a valid TLS/SSL certificate and also configure it correctly in project, then it will work without the above error.

  • 2
    This is a borderline [link-only answer](//meta.stackexchange.com/q/8231). You should expand your answer to include as much information here, and use the link only for reference. – Nick is tired Oct 29 '17 at 15:15
4

I guess this is a generic websocket issue.

Change the url to a dynamic name using the built-in location.host variable and change the protocol to secure websocket wss if you have set-up the TLS:

const ws = new WebSocket("wss://" + location.host + "/")
go je jo
  • 311
  • 4
  • 8
4

also you could easily change the mappings of IP addresses to host names, on windows go to C:\Windows\System32\drivers\etc\hosts and uncomment this line

127.0.0.1       localhost

save and restart.

monim
  • 3,641
  • 2
  • 10
  • 25
3

try to change the port to 8080

const ws = new WebSocket('ws://localhost:8080/chat')
fadi omar
  • 740
  • 5
  • 15
  • What is the significance of `chat` here? Wouldn't that be invalid port? – Esqarrouth Jun 16 '20 at 15:37
  • 1
    it's the path in the wesockets server server url, e.g. /chat for your chat messages, /matches for your tinder matches, etc. probably for some scaling concerns – Dorian Sep 26 '21 at 21:28
3

Port 9090 is used by reactotron. Probably you are using it in your project and your app cannot connect with reactotron because it is closed. Just open reactotron and the error will disappear.

1

maybe you forgot to start websocket server, check it again, with configuration in my project, run:

php artisan websocket:init