Socket programming with PyQt - multithreading

I would like to write a simpe program that can handle a server-client connection (1:1 connection is enough) in PyQt. I want to send a message from the server to the client and then reply to server with another message.
I have found a chat application that uses PyQt.
Here is the link:
http://www.mranuran.com/blog/2017/08/04/creating-graphical-chat-application-using-pyqt-and-socket/
I have tried the code, but unfortunately it did not work. I ran the server.py first and then the client.py. But something was wrong with the connection. This line:
print(“[+] New server socket thread started for ” + ip + “:” + str(port))
was never executed.

Related

Using `io.sockets.emit` in the router. What if socket establishing is not finished before it?

I'm using io.sockets.emit in the router like
db.SomeModel.find({},
function(err, modelDate) {
io.sockets.emit('eventName', modelData);
}
);
What would happen if a socket took like 10sec (just an example) to be established, and before it is established I try to emit something. Would it give some undefined error or..? I'm new to node and asynchronous programming in general. Thanks a lot.
What if socket establishing is not finished before it?
It will just be skipped and no data will be sent to a socket that is has not yet established its connection.
io.sockets.emit() loops through a list of connected sockets and sends to each one individually. If a socket is not done connecting, then it won't be in the list that socket.io is iterating through so no data will be sent to it.

SocketIO connection stop sending data after 4-5 hours

I have developed an application with ReactJS, ExpressJS, MongoDB and SocketIO.
I have two servers:- Server A || Server B
Socket Server is hosted on the Server A and application is hosted on the Server B
I am using Server A socket on Server B as a client.
Mainly work of Server A socket is to emit the data after fetching from the MongoDB database of Server A
Everything is working as expected but after 4-5-6 hours stop emitting the data but the socket connection will work.
I have checked using
socket.on('connection',function(){
console.log("Connected")
)
I am not getting whats the wrong with the code.
My code : https://jsfiddle.net/ymqxo31d/
Can anyone help me out on this
I have some programming errors.
I am getting data from MongoDB inside the setInterval() so after a little while exhausts resources and database connection starts failing every time.
Firstly i have created a Single MongoDB connection and used every place where i needed.
2ndly i removed setInterval and used setTimeout as below. (NOTE: If i continue using setInterval it execute on defined interval. It doesn't have any status that the data is emitted or not [this also cause the heavy resource usages] but i need to emit the data to socket when successfully fetched.)
setTimeout(emitData,1000);
function emitData(){
db.collection.find({}).toArray(function(data){
socket.emit('updateData',data);
setTimeout(emitData,1000);
})
}

Solving Socket.IO socket undefined on connection from Arduino?

I'm trying to connect to socket.io from arduino. The service works from the browser, but if I try to connect from the arduino, when I look in the node log I created via console.log when a connection happens, the socket is undefined, but picks up the connection. Below is the protocol switching request, which returns a 404 not found, but socket.io does register a connection but doesn't define the socket.
client.print(F("GET /socket.io/1/websocket/"));
client.print(sid);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.print(hostname);
client.fastrprint(F(":"));
char portBuffer[10];
itoa(port, portBuffer, 10);
client.fastrprint(portBuffer);
client.print(F("Sec-WebSocket-Key: ")); //dGhlIHNhbXBsZSBub25jZQ==
client.print(F("dGhlIHNhbXBsZSBub25jZQ=="));
client.println(F("Origin: ArduinoSocketIOClient"));
client.println(F("Sec-WebSocket-Protocol: chat, superchat"));
client.println(F("Sec-WebSocket-Version: 13"));
client.println(F("Upgrade: websocket"));
client.println(F("Connection: Upgrade\r\n"));
I was thinking maybe I formed the request incorrectly, but I can't find any information for the socket.io protocol switching request so I don't know what to change. What I have done is loosely based on what I read in web socket protocol stand.

What is the purpose of `socket.broadcast.to(param)` in socketio

While I'm learning Node.js, I confronted an example to write a chat system.
Somewhere in the code there's following line:
socket.broadcast
.to(message.room)
.emit('message', theMessage);
I don't understand what the to function is doing. Also, I didn't find any clue at the client side code. What happens if the code has not to(message.room) part?
socket.broadcast.to broadcasts to all sockets in the given room, except to the socket on which it was called.
For more details : http://socket.io/docs/server-api/#socket#to(room:string):socket

Get remoteAddress from https.Server#clientError in Node.js

In Node.js both http.Server and https.Server emit the clientError event but with different arguments:
http.Server#clientError(exception, socket)
https.Server#clientError(exception, securePair)
With securePair being an instance of tls.SecurePair, securePair.cleartext a tls.CleartextStream and securePair.encrypted a tls.CryptoStream.
The question is: how do I get the address and port of the client that caused the clientError? In theory, this should work:
socket = securePair.cleartext.socket;
console.log(socket.remoteAddress + ':' + socket.remotePort);
In reality, when I try to connect to the HTTPS server using HTTP (pretending it were a HTTP server) and cancelling after a few seconds, I get a clientError of type ECONNRESET, but socket.remoteAddress and socket.remotePort are both undefined (even though securePair.cleartext.socket is indeed a net.Socket and not undefined).
Unfortunately, bounty did not seem to help much :-( I had to investigate this myself.
Apparently, a fix has been made (but is not yet in any released node.js) to be able to get these values even after socket has been closed: https://github.com/joyent/node/commit/8c38b07252d86f4eef91344f8df4f0c3e38bca35
A workaround is to store the address at connect time:
srv.on('connect', function (socket) {
socket.stored_remoteAddress = socket.remoteAddress;
});
srv.on('clientError', function (err, socket) {
console.log(socket.cleartext.socket.stored_remoteAddress);
}
This works, but is not pretty.
Apparently, even this does not work fully :-(
Node.js 0.11 does not emit the 'clientError' event on SSL errors (for example), so there's no way to catch the situation there.
Node.js 0.12 emits the 'clientError' event, but uses a new TLS implementation which does not have the 'cleartext' socket available anymore. I could not find any way to pass information from 'connect' event to 'clientError' event, or any way to find out that they are for the same socket (as the socket is already closed in the 'clientError' event).
So, it would seem that the only way is to wait for a new Node.js release (0.12.2 earliest, if they think this is worth a fix) and hope this gets fixed for real.

Resources