Keeping connection open in Django without websockets - django

I have a mini computer that does not support websockets that I would like to receive push notifications from a server.
The issue is that after the client connects to the server, the server responds and then closes the connection. This makes it so the client has to continually reconnect to the server to get new information.
Is there a way using Django to allow the connection to be left open and then have the server publish data to the client?

Django is primarily a request/response framework and as such does not have support for real duplex communication.
Socket.IO is the de facto library that makes websocket-like functionality cross-browser (IE5.5+), using real websockets as a transport if the browser allows it, falling back to HTTP long-polling or whatever else if it doesn't. For various options on integrating Socket.IO with Django, read this.

Related

Django channels and socket.io-client

I'm trying to use them for the first time and wonder I'm headed to the right direction.
Here are my understandings,
socket.io is a wrapper around websocket, and falls back to sub-optimal solutions when websocket is not available.
Django channels can talk websocket as well.
(I think it converts django as a message queue like system. although this understanding or misunderstanding should affect this question)
So I'm trying to use Django channels on the server and socket.io-client on the client.
socket.io has api which looks like
socket.on(type, (payload)=> {})
Whereas Django channels has a form of
message.reply_channel.send({
"text": json
})
is the "text" type of socket.on(type)?
Can Django channels and socket.io-client talk to each other?
From the Socket.IO README:
Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either. Please see the protocol specification here.
So, you shouldn't expect Channels to work directly with Socket.IO. Global browser support for websockets is at 93%, which is probably high enough to just use the websocket API directly.
To quote the creator of django channels: https://github.com/django/channels/issues/1038
Channels doesn't support socket.io - it's a different protocol that isn't websockets or HTTP but layers on top of them. You'll have to use a socket.io server if you want to use it.

How to make Qt Websocket and QNetworkRequest (HTTP) to use the same connection?

Is it possible with Qt to upgrade a HTTP connection that handles the normal HTTP requests to a Websocket with the same connection?
I'm thinking about something like this with Poco libraries, but all done in Qt similar to QtWebApp.
The simple answer is no and that is mostly because of specifics of the server side. And Qt just follows the protocol available and exposed by the server (HTTP/WebSocket) as mostly the client-side development framework and AFAIK won't be able to do the kind of transformation you want of going from HTTP to Websocket that are two different protocols. But of course, theoretically that can be done as long as both protocols able to use IP port 80. But that implies new unique sever and new unique client implementations.
We use both WebSocket and REST in our app. And WebSocket is for triggering the client by the server to do something. Client gets the "poke" from the server and starts normal JSON HTTP-based exchange with the server.
Somewhat relative link: https://softwareengineering.stackexchange.com/questions/276253/mixing-rest-and-websocket-in-the-same-api

How to get a WebSocket server run on aws

I'm developing an iOS app that requires realtime dual-way server/client messaging.
I'm trying to use WebSocket++ to develop a WebSocket server app on an AWS EC2. Have to use C++ because that's the only language I know on the server side.
The problem is I'm a fresh guy on server side development. I have 2 very basic questions:
1, Do I need have to setup an HTTP server like apache/nginx in order to get websocket running?
That is, can websocket app live independently alone?
2, I have now setup an nginx server in case it is a must have, is there any resource that I can refer to to make nginx & websocket work together well?
No, you don't need a Web server, a (reverse) Web proxy or anything to have your C++ WebSocket server talk to WebSocket clients.
Nginx (as HAproxy) supports reverse proxying WebSocket. This can make sense in certain situations, like you want to terminate TLS at the proxy and forward plain WebSocket to your backend server, or you want to load-balance incoming WebSocket connections to multiple backend nodes. However, as said, this isn't required.
No you don't, websocket and socket for an HTTP server are two diffent things.
HTTP server is for the HTTP protocol while there is not protocol defined for websocket, you have to define it yourself typically by the mean of sending/receiving Json message (a stream of character which each side (the server and the client) knows how to read/write).
The goal of websocket is to offer to javascript through HTML5 an easy, light and quick way to communicate through a socket, without websocket you have to do that with web services and in that case you need a http server.
With websocket you can create an html file leveraging html tag and javascript, javascript use client side of websocket to communicate with a C++/websocket server program, and you do not need even a web server, in this scenario you have a "desktop web app" ! (here web term is only because you use html tags)
Same question, same answer, no again ;-)
Good luck, and welcome in the wonderful world of asio !

Difference between a web-service and web-socket

As I mentioned in the title: I would like to know the difference between the web-service and the web-socket? when we used each one?
Thanks!
A web service is an HTTP server that responds to client SOAP/REST/JSON requests.
A web socket is a client-side API that allows a web browser to create a bidirectional communication link with a server without having to change/reload the current page. This is typically used for AJAX requests to dynamically update live content on the current page, or create chat sessions between clients, or implement custom protocols that run in the web browser.
Web services are based on HTTP protocol and use HTTP methods to relay data in a request and response paradigm. Thus the client will always be the one responsible for communicating with the server, requesting data and submitting data to the server i.e getting list of customers or products, adding products or customers to server.
In contrast, Web sockets allow bidirectional communication, meaning server can initiate communication as much as client can do the same. Typically you supply a host IP Address and port to the socket. Web sockets can be used to implement a chat application.
The key difference between Web sockets and Web services is that with web sockets you get bi-directional connection in which the server and client can continuously send messages back and forth while Web services are uni-directional connection concerned with supplying clients with resources

How to send frequent real-time server-side status update messages?

I have a web server that listens for jQuery keyboard events and I would like to send a string of the status for a set of keys to another domain for processing.
What technology/language/protocol is able to do this on the server-side?
It must be able to send the updated status message upon immediate change as listened for by keyboard events like arrow keys.
This is not a standard server <-> client updating but rather client > server > another server.
Can a WebSocket be used to create a server-side connection to a remote location, not a client?
https://en.wikipedia.org/wiki/WebSockets
What technology/language/protocol is able to do this on the server-side?
Any modern server side language can do it, node.js, c#, python, php. Just write your condition and make the request from your server as client.
Can a WebSocket be used to create a server-side connection to a remote location, not a client?
Of course! Your server can be a client like any other device. NodeJS example