-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
question about Http Server integration #5
Comments
Hello, You should be able to use golongpoll either way: as its own web server, or wrapped by your own server. I have not personally used gin, but from a quick look it appears possible to do what you are asking. If you want to use longpolling inside of URLs mapped by the gin framework, then you could set up a URL-to-function mapping just like before, but have that function wrap the longpoll manager via a closure. So from the gin example I found here: http://phalt.co/a-simple-api-in-go/ package main
import (
"github.com/gin-gonic/gin"
)
func index (c *gin.Context){
content := gin.H{"Hello": "World"}
c.JSON(200, content)
}
func main(){
app := gin.Default()
app.GET("/", index)
app.Run(":8000")
} You could integrate longpolling with something along the lines of this: package main
import (
"github.com/gin-gonic/gin"
"github.com/jcuga/golongpoll"
)
func Index (c *gin.Context){
content := gin.H{"Hello": "World"}
c.JSON(200, content)
}
func GetEvents(manager *golongpoll.LongpollManager) func(c *gin.Context) {
// Creates closure that captures the LongpollManager
return func(c *gin.Context) {
// TODO: use c (gin.Context) if you need to
// ...
// NOTE: the gin.Context wraps a http Request and Writer
// Now pass request/writer to the sub handler to get longpoll events
// This will fufill the longpoll subscription request
manager.SubscriptionHandler(c.Writer, c.Request)
}
}
func GetPublish(manager *golongpoll.LongpollManager) func(c *gin.Context) {
// Creates closure that captures the LongpollManager
return func(c *gin.Context) {
// TODO: use c (gin.Context) if you need to
// ...
// Publish a longpoll event on a specific subscription category:
some_event_data := c.Request.URL.Query().Get("some_data")
manager.Publish("some-event-category", some_event_data)
}
}
func main(){
app := gin.Default()
app.GET("/", Index)
// TODO: all your other gin URLs-to-Functions
// ...
// Add longpolling via [ the awesome :) ] golongpoll library
app.GET("/events", GetEvents(lpManager))
app.GET("/publish", GetPublish(lpManager))
app.Run(":8000")
} Notice that the gin.Context type wraps the Request and Writer types: https://godoc.org/github.com/gin-gonic/gin#Context And note that the longpoll manager's subscription handler (https://godoc.org/github.com/jcuga/golongpoll#LongpollManager) takes the Writer and Request as args. I haven't bothered trying this snippet out or compiling it, but something along these lines would work. |
And yes, to add some simple notifications to a webapp avoiding the extreme overkill that is websockets is a good call. |
If this works out, I should probably clean up my above example and put it in the documentation... |
@jcuga Yes, if you have time, could you put in a new example with this GIN code ?? If not i understand. |
thanks @jcuga i am trying it out now.. cheers |
This looks pretty useful for my use case.
I have a restful interface at the moment, and need to push things to the Web Client.
I want to avoid Web sockets and too much complexity.
So, i am wondering about If the Restful Server i have , (based on Gin) needs to be a separate service from the Long Poller. I ask this because for my Restful interface i have alot of routing URLS already setup, and i am not sure if i can just all the Long Poller as a path on the standard Gin Router or not.
I know this is maybe hard to answer and i should just give it a go, but thought it was worth asking first.
thanks in advance.
The text was updated successfully, but these errors were encountered: