Coder Social home page Coder Social logo

"welcome" event about go-sse HOT 11 OPEN

gregfr avatar gregfr commented on August 25, 2024
"welcome" event

from go-sse.

Comments (11)

gregfr avatar gregfr commented on August 25, 2024 3

Well I would definitely have the use for it :)

from go-sse.

alexandrevicenzi avatar alexandrevicenzi commented on August 25, 2024

No, there's no hook for it. That being said, it would be possible to add a hook for that, but not sure if this would look good or be useful.

from go-sse.

alexandrevicenzi avatar alexandrevicenzi commented on August 25, 2024

Feel free to propose a change, or send a PR, but first I would like to see how it would look like.

from go-sse.

gregfr avatar gregfr commented on August 25, 2024

Well when you have several clients on a channel, you could "greet" each one with some specific info, like an internal reference that the server uses to specifiy the client, or some kind of JWT for example.
Ideally, it could be returned when the clients connects, like:

app.$sse(url,{options})
.then((see, greetings) => {
jwt = greetings.jwt;
myID = greetings.yourID;
});

from go-sse.

gregfr avatar gregfr commented on August 25, 2024

On the server side it could be:

s := sse.NewServer(&sse.Options{
...
  Greeter: func() {
  }
}

I realize this is inherently server-client so both sides should be involved, however.

from go-sse.

alexandrevicenzi avatar alexandrevicenzi commented on August 25, 2024

@gregfr I don't like this approach using the Server Options.

My idea would be to do something more close to what you seen in logrus, a function called server.AddHook for example.

Another issue is that I don't want only one event. If there's an event for client connected there should be for client disconnected as well. On that yet, I think that could also be nice to have Pre and Post event, but that's another discussion, for now, Post event would do the job for most use cases that I'm aware of.

from go-sse.

alexandrevicenzi avatar alexandrevicenzi commented on August 25, 2024

@graywolf336 you can also share some ideas here as this would solve your question on #23.

from go-sse.

The-One-Law avatar The-One-Law commented on August 25, 2024

Greetings,
I am also considering this,
perhaps better to just poll for new clients between sendmessages
and (when found) simply move them to appropriate channels ?
(currently exploring how to do this - In order to validate clients)

from go-sse.

graywolf336 avatar graywolf336 commented on August 25, 2024

One thing I really like about gorilla websockets framework is their upgrade function returns a pointer to the client, so I wonder if that would be a viable solution here. That way when a client is connected, it is returned and I can send over the messages.

Aside from that approach, you could take the AddHook(eventType, func) approach and that wouldn’t be too bad.

type EventType string

var (
  EventTypeBeforeConnect EventType = “before-connect”
  EventTypeConnected EventType = “connected”
)

Naming could be better though.

from go-sse.

alexandrevicenzi avatar alexandrevicenzi commented on August 25, 2024

I like the idea of sending the object when a hook is triggered, but with generic AddHook function you would need to receive an interface and cast it.

AddHook is a nice way because we could add more events without breaking anything, but there's this downside of casting objects as there's no way to have function overload.

One reason that I never implemented this is that I never found a clean/good way of doing, neither I know the most go-way of doing it.

Looking for examples in other libraries is a good idea, as web socket is more popular than SSE.

from go-sse.

graywolf336 avatar graywolf336 commented on August 25, 2024
func handler(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Println(err)
        return
    }
}

That is how gorilla websockets framework handles it. So, if you upgrade the connection and then you get back the connection which can be used to send and receive messages.

That is what I think would be the ideal way to approach it. Provide a way to "convert" or "upgrade" the connection to an sse client and send the "welcome" message that way.

from go-sse.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.