rawhat / glisten

A pure gleam TCP library
Apache License 2.0
55 stars 8 forks source link

Send bytes immediately on new connection #16

Closed nicklanng closed 5 months ago

nicklanng commented 5 months ago

Hi there! Just been experimenting with Gleam since the announcement. My first project is to recreate my basic MUD server. As part of that, I'd like to send some control bytes to the client immediately as it connects, without waiting for a message from the client first. I couldn't quite work out if this is possible. Thanks!

rawhat commented 5 months ago

That's a great question. I think with the current architecture this is not possible. I could definitely see that being useful.

I'll think about this a bit more, but I wonder if something like an optional on_connect (or some other name) function you can pass in that gets called when the connection starts.

Something like:

glisten.handler(...)
|> glisten.on_connect(fn(state, conn) {
  glisten.send(conn, ...)
})
|> ...

Would that resolve this for you?

nicklanng commented 5 months ago

Yeah I think that would be perfect!

nicklanng commented 5 months ago

Another option I thought about was trying to use the init function passed into the handler function, making the connection an argument to that init function. I'm just not sure whether the connection even exists at that point, if the actor is created in the pool and then assigned a connection as one comes in.

rawhat commented 5 months ago

Haha yeah, I also came to the same conclusion. I was over-thinking it before. Thanks!

rawhat commented 5 months ago

I also realized this is how mist handles it with WebSockets. So definitely just an oversight on my part.

Thanks for the issue! This is merged on master. I'll need to do a major version release, which I'll probably just do soon. I don't really have anything else in mind / ready to go to warrant waiting.

If you're interested, you can definitely clone this and use it as a path dependency. Otherwise, like I said, should be available soon!

nicklanng commented 5 months ago

Hey @rawhat , would you mind pushing this new version, please? I need it now for my docker build and #17 should be solved by it.

rawhat commented 5 months ago

Yep this is done now.