FastAPI websocket ping/pong timeout - websocket

I am using FastAPI with #app.websocket to listen for incoming websockets. How does FastAPI (or Starlette or Uvicorn underneath) do ping/pong heartbeats? Is this configurable? I cannot find it in the documentation at all.
from fastapi import FastAPI, WebSocket
app = FastAPI()
#app.websocket("/")
def ws(websocket: WebSocket):
pass
fastapi uses starlette, and under the hood it seems to use websockets. websockets.connect by default uses a ping_interval and ping_timeout of 20 seconds, but I can't tell if that is used in FastAPI.

You can now use the 2 following flags:
--ws-ping-interval <float> - Set the WebSockets ping interval, in seconds. Please note that this can be used only with the default websockets protocol.
--ws-ping-timeout <float> - Set the WebSockets ping timeout, in seconds. Please note that this can be used only with the default websockets protocol.

Related

In ActiveMQ WebSockets Is there a way to a allow a grace period on Heartbeats?

We are using STOMP over WebSockets (ref) into ActiveMQ 5.15.
With this we are defining a client (to server) heartbeat of 20 seconds. The notification of this time is built into the STOMP protocol so communicated to the server.
CONNECTED server:ActiveMQ/5.15 heart-beat:0,20000 session:ID:app.server.mycompany.com-43039-1617089631808-3:3585 version:1.1
The client heartbeat code depends on browser setInterval() and this doesn't appear fully reliable so occassionally the heartbeat doesn't get sent and the server then ends up killing the connection.
Transport Connection to: ws://10.x.x.x:50670 failed: org.apache.activemq.transport.InactivityIOException: Channel was inactive for too (>20000) long:
I have noticed that there is a transport.hbGracePeriodMultiplier setting on pure Stomp connector in ActiveMQ however the Stomp over WS client connects in on ActiveMQ WebSocket connector.
My question is: is there an equivalent grace period setting for WebSockets?

MQTT over websocket in c

I have implemented mqtt using server connection tcp socket on my machine with mosquitto broker. I have totally understood the mqtt protocol and its frame format. I want to publish my data over webserver which supports mqtt over websocket.
How can I start with this thing?
I am not clear with websocket concept
Can I implement websocket using tcp or is there any other method.
do i have to use http to implement mqtt over web socket as to send data over webserver?
As http and mqtt use different methods to send or receive data.
I don't want to use ready libraries such as paho.
I am totally new to this socket programming.any help or guideline will greatly appreciated!!!
Websockets are an extension to the HTTP protocol, you need to use a correctly formatted HTTP request to setup a new Websocket connection.
Once the connection is setup it can be used to send the exact same binary MQTT packets that you would send over an existing TCP connection.
I suggest you look at using an existing library like libwebsockets to handle the Websocket connection setup, then you should be able to interface your existing code to just use the websocket handle instead of the socket handle.
If you REALLY don't want to use a library then you will need to start by reading the Websocket RFC https://www.rfc-editor.org/rfc/rfc6455

GKE + WebSocket + NodePort 30s dropped connections

I have a golang service that implements a WebSocket client using gorilla that is exposed to a Google Container Engine (GKE)/k8s cluster via a NodePort (30002 in this case).
I've got a manually created load balancer (i.e. NOT at k8s ingress/load balancer) with HTTP/HTTPS frontends (i.e. 80/443) that forward traffic to nodes in my GKE/k8s cluster on port 30002.
I can get my JavaScript WebSocket implementation in the browser (Chrome 58.0.3029.110 on OSX) to connect, upgrade and send / receive messages.
I log ping/pongs in the golang WebSocket client and all looks good until 30s in. 30s after connection my golang WebSocket client gets an EOF / close 1006 (abnormal closure) and my JavaScript code gets a close event. As far as I can tell, neither my Golang or JavaScript code is initiating the WebSocket closure.
I don't particularly care about session affinity in this case AFAIK, but I have tried both IP and cookie based affinity in the load balancer with long lived cookies.
Additionally, this exact same set of k8s deployment/pod/service specs and golang service code works great on my KOPS based k8s cluster on AWS through AWS' ELBs.
Any ideas where the 30s forced closures might be coming from? Could that be a k8s default cluster setting specific to GKE or something on the GCE load balancer?
Thanks for reading!
-- UPDATE --
There is a backend configuration timeout setting on the load balancer which is for "How long to wait for the backend service to respond before considering it a failed request".
The WebSocket is not unresponsive. It is sending ping/pong and other messages right up until getting killed which I can verify by console.log's in the browser and logs in the golang service.
That said, if I bump the load balancer backend timeout setting to 30000 seconds, things "work".
Doesn't feel like a real fix though because the load balancer will continue to feed actual unresponsive services traffic inappropriately, never mind if the WebSocket does become unresponsive.
I've isolated the high timeout setting to a specific backend setting using a path map, but hoping to come up with a real fix to the problem.
I think this may be Working as Intended. Google just updated the documentation today (about an hour ago).
LB Proxy Support docs
Backend Service Components docs
Cheers,
Matt
Check out the following example: https://github.com/kubernetes/ingress-gce/tree/master/examples/websocket

RabbitMQ Web STOMP without SockJS

Is it possible to setup RabbitMQ Web STOMP connection without SockJS library?
I have played around with rabbitmq-web-stomp plugin without a success as the initial response generated by the server is Welcome to SockJS! (which is obviously not a STOMP based message).
Is SockJS really required? What does it bring into the game (besides legacy browser support)?
SockJS protocol does support raw WebSocket clients under /websocket path.
Any SockJS server complying with 0.3 protocol does support a raw WebSocket url. The raw WebSocket url for the test server looks like:
ws://localhost:8081/echo/websocket
You can connect any WebSocket RFC 6455 compliant WebSocket client to this url.
This is supported by Rabbit's implementation as well. So the default endpoint is: http://example.com:15674/stomp/websocket.

heroku socket connection limitation

One of my app need socket connection.
I tested on nitrous.io, but their free account always close the socket connection if there is no signal between server and client more than 1 minute.
Just wonder if there is similar socket connection limitation on heroku?
Your comment welcome
Heroku has support for WebSockets, with the usual 30s timeout limitation. This can be easily worked-around by sending heartbeat packets.
https://devcenter.heroku.com/articles/heroku-labs-websockets

Resources