26
votes

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.

7
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
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
can u give me the better tutorials link ? @sidewinder i found this tutorial very simple as i am a beginner - Hafsa
If you can give me some better links for webRTC tutorial to develop an application, i will be glad @sidewinder - Hafsa

7 Answers

16
votes

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.

6
votes

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

2
votes

try to change the port to 8080

const ws = new WebSocket('ws://localhost:8080/chat')
2
votes

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
votes

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.

0
votes

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 + "/")
0
votes

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.