Python WebSocket is not working on Raspbery - javascript

Running the server and client on mac with localhost everything works fine.
Running the python program (server) on the raspberry pi and try to access it using its url doesn't work.
Python Server:
class Strompreisgenerator:
def __init__(self):
self.ws = websockets.serve(self.echo, 'localhost', 5001)
asyncio.get_event_loop().run_until_complete(self.ws)
asyncio.get_event_loop().run_forever()
async def echo(self, websocket, path):
async for message in websocket:
print(message)
Javascript Client:
var ws = new WebSocket("ws://www.tobiasschmocker.ch:5001");
While trying to instantiate the WebSocket the error "WebSocket network error: The operation couldn’t be completed. Connection refused" occurs in safari.
The Port 5001 is open on RPi. I also tried local IP. I forwarded the Port on my router but still nothing. If i trie other urls i get another error, so i suppose the url is correct but i have no rights somehow.
On my RPi i have ssh enabled, also php, apache, mysql and all the pip packages for my python server.
If you know, where the problem lies, i'd be happy to know. Thank you very much!

Now the websocket is running and available via url from external!
Following is the solution for completeness. All I had to do is:
Update to Python Version 3.6. The websockets package seems to work only from Python Version 3.6. Here's the tutorial I used to install Python 3.6 on my RPi: https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f
use 0.0.0.0 instead of localhost as host within python. On sudo netstat -lptu in the RPi terminal, I could see that all the other ports (mqtt, ssh, etc) where set to 0.0.0.0 instead of localhost so I just tried and it works.
Having set all that, the websocket is now running. Thanks to #YonatanKiron & #Reto!

Related

NodeJS - How to connect to remote PC that runs Node app?

I have a Nodejs client app, and a Nodejs server app. I use Apollo GraphQL for network.
These communicate fine if run on the same pc. They also work fine if the client is on another pc and server on my pc, over LAN only. They fail to communicate via internet.
My code that works right now:
server:
server.listen(PORT, '0.0.0.0',() ...
client:
const wsLink = new WebSocketLink({
uri: 'ws://192.168.10.41:8081/subscriptions',
Firs of all you must be sure that both nodes have a public IP, otherwise if you are behind a symmetric NAT you will not be able to do that.
After that you can use some of the modules of node, for example UDP(user datagram protocol) to try the connection.
Good Luck

WebSockets working on localhost, but do not work on remote Ubuntu host

I have two applications - browser based client and NodeJS based server that are both communicating using WebSockets (I'm using ColyseusJS library). Problem is, that everything works fine while I'm testing them on localhost but when I deploy the application to my Ubuntu VPS server it stops working.
The message I receive in the browser while trying to connect is:
WebSocket connection to 'ws://X.X.X.X:8001/?colyseusid=' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
So it reaches the server (because when server is disabled the message is "Error in connection establishment: net::ERR_CONNECTION_REFUSED") but it looks like it fails on Upgrade connection operation.
What is strange is that I managed to make it work yesterday (don't know how exactly), but had so much mess on my VPS that I decided to revert machine to it's starting state. After that it stopped working (code is unchanged). Maybe there are some additional dependencies that I need to install in order to make it work on my Ubuntu Server?
I would really appreciate your help.
Have you tried to port forward the port to the ip address of the said VPS in your router config? And also check out the firewalls.

WebSocket connection failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

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.
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.
You need to use ws://yourIp:9090/, where yourIP is like 192.168.?.?.
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.
try to change the port to 8080
const ws = new WebSocket('ws://localhost:8080/chat')
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 + "/")
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.
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.
WebSocket connection to 'ws://localhost:8080/' failed
Must ensure server file is running
I git this above problem
maybe you forgot to start websocket server, check it again, with configuration in my project, run:
php artisan websocket:init

Can't get socket.io to work on server, while running fine locally

This code is running fine with the node.js server running locally:
Server:
var client = require('socket.io').listen(3001).sockets;
Client HTML:
<script src="http://localhost:3001/socket.io/socket.io.js"></script>
Client Javascript:
var socket = io.connect('http://localhost:3001');
Then I moved the server.js to my ubuntu server which has node and socket io installed, using the same code, and started it without error.
Clientside I changed the local html/javascript file from localhost:3001 to ServerIP:3001 and it just doesn't work anymore. When I check firebug, I can see it never finishes trying to GET http://ServerIP:3001/socket.io/socket.io.js. It does not fail, just never finishes.
What did I do wrong? Thank you.
This is most likely a firewall issue.
Allow connections on port 3001 using the following (if you use iptables)
iptables -A INPUT -p tcp --destination-port 3001 -J ACCEPT
Than try again

How to connect to a Mosquitto broker on a Raspberry Pi through web sockets?

I am trying to connect to a Raspberry Pi that has the Mosquitto broker installed. The client on the RPi is connected using:
client.connect("127.0.0.1", 1883, 60)
I tried to connect to it on my MQTT JavaScript client using the following specifications but I failed:
client = new Paho.MQTT.Client("10.101.125.190", 1883,"myclientid_" + parseInt(Math.random() * 100, 10));
I also tried changing the port to 8080 from the JavaScript side but I still failed. If I change the port to 8080 on the RPi then it won't even connect.
This is the error that I am getting at the moment:
WebSocket connection to 'ws://10.101.125.190:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
So, what do I need to change to fix this error? The RPi and my JS client are both in the same local network.
Edit:
I forgot to mention that I have already tried this with test.mosquitto.org - 8080 and it worked, but as soon as I change the address then I start getting the error.
MQTT over websockets does not share the same port as native MQTT.
You will need to add a new listener to your mosquitto config.
You will need to add something like the following to the end of your /etc/mosquitto/mosquitto.conf (or in a seperate file in /etc/mosquitto/mosquitto.d)
listener 1884
protocol websockets
Then need to update your JavaScript to connect to port 1884 not 1883
You will also need to be using a version of mosquitto newer than than 1.4.x iirc the default version that is packaged for raspbian is too old. Follow the instructions here to get a newer version.

Categories

Resources