I have been trying to develop an IoT-based application. I thought using WebSockets with my Raspberry Pi Pico W as the server would be a good idea to transfer information to my react application in real-time. I am new to socket programming and I am having issues transferring information between the React App and the Pico W. For some more context, my Server side code looks like the following:
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('Listening On ' + str(addr))
while True:
try:
cl,addr = s.accept()
print('Client connected from ' + str(addr))
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send("Hello")
time.sleep(3)
cl.close()
except:
cl.close()
break
In React Native Client-side code looks like follows:
import io from "socket.io-client"
import {useEffect } from 'react';
export default function App() {
useEffect(() => {
const socket_2 = io("http://<IP_ADDRESS_NOT_SHOWN_HERE>:80")
socket_2.emit('message', 'data');
socket_2.on('message', (data) => {
console.log(data);
});
console.log(socket_2)
}, []);
The interesting thing is that I am sure the server side is working because when I connect from the browser is sends me the "Hello" response. Additionally when I connect from React App I see a bunch of logs in Python Resembling the following:
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62518)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62540)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62541)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62554)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62567)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62573)
Client connected from ('<IP_ADDRESS_NOT_SHOWN_HERE>', 62580)
To sum it up there are two primary things I am questioning here.
1-) I am not sure why it keeps making new connections
2-) I am also not sure why my information is not being transferred in between React Native and my Raspberry Pi Pico W server properly
Any help would be appreciated I am a socket-programming noob :/
I have tried using Socket IO and also I have tried using the react native socket library. I believe the socket io is much easier but I just cannot understand what is happening here