Comments (9)
Hi @gedw99, thanks for the interest.
In my original design, I wanted the client to server communication to be over the same request stream as the SSE. I had the following demo code working locally.
However, when I started testing in Kubernetes, I was not able to get this working through my Ingress controller (Traefik in my case); I had validated through kubectl portforward
that the connection still worked correctly. I have not tested other ingress controller yet.
I am also not sure whether this full duplex would work without HTTP 2, as some proxies I use don't support that. So I decided to split the client to server communication from the server to client stream.
I don't have much experience using SSE between front-end and back-end; I have always used Websockets. So I am curious how other people use SSE.
Server Request Handler
fullDuplexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// First flash response headers
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
// Copy from the request body to the response writer and flush
// (send to client)
if _, err := io.Copy(flushWriter{w: w}, r.Body); err != nil {
log.WithError(err).Error("Full-Duplex")
w.WriteHeader(500)
}
})
Client Side
// Create a pipe - an object that implements `io.Reader` and `io.Writer`.
// Whatever is written to the writer part will be read by the reader part.
pr, pw := io.Pipe()
client := http.Client{}
// Create an `http.Request` and set its body as the reader part of the
// pipe - after sending the request, whatever will be written to the pipe,
// will be sent as the request body.
// This makes the request content dynamic, so we don't need to define it
// before sending the request.
resp, err := client.Post(args.URL, "text", ioutil.NopCloser(pr))
// Send the request
log.Infof("Send POST to %s", args.URL)
if err != nil {
return (err)
}
log.Infof("POST Got: %d", resp.StatusCode)
// Run a loop which writes every second to the writer part of the pipe
// the current time.
go func() {
for {
log.Info("Writing to server")
time.Sleep(time.Second)
fmt.Fprintf(pw, "It is now %v\n", time.Now())
}
}()
// Copy the server's response to stdout.
_, err = io.Copy(os.Stdout, resp.Body)
from httpmq.
I have used SSE . Pocketbase a popular golang project used them. Can probably look at that later if we want to give it another try .
Do you currently use web sockets instead over http2 ?
from httpmq.
PocketBase looks like a very interesting project. I will take a look later.
I use websockets in my other projects. This project uses HTTP1.1 / HTTP2.
from httpmq.
So there is no push currently
Mhh . Best to use nats
from httpmq.
Unfortunately, there is no plans to transition to web-sockets at the moment.
I am using this project as apart of another project, so the design decision is partly driven by the needs of that project.
from httpmq.
Ok thanks
I think I can extend it without forking to incorporate nats.
from httpmq.
Sounds good. Let me know if you have any other questions about the project.
from httpmq.
Unfortunately, there is no plans to transition to web-sockets at the moment.
I am using this project as apart of another project, so the design decision is partly driven by the needs of that project.
Yep makes sense ...
But i realised i was lazy with me writing....
I meant the inherent websockets of NATS Jetstream can be turned.
For the data plane it's just a matter of turning it on. THen clients no nats.go dependency can get events sent to them from nats.
I currently use nats.go for projects of Web clients, Desktops and Servers. The Web client is the nats.go compiled to wasm. Call me pessimistic but i wanted feature parity for Web and Non web clients to use the same code.
from httpmq.
Unfortunately, there is no plans to transition to web-sockets at the moment.
I am using this project as apart of another project, so the design decision is partly driven by the needs of that project.
I meant the inherent websockets of NATS Jetstream.
For the Control plane it's just a matter of turning it on.
I currently use this for Web clients, Desktops and Servers. The Web cleit is the nats.go compiled to wasm. Call me nuts but i wants the Web and Non web clients to use the same code..
from httpmq.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from httpmq.