Comments (5)
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.
from golongpoll.
And yes, to add some simple notifications to a webapp avoiding the extreme overkill that is websockets is a good call.
from golongpoll.
If this works out, I should probably clean up my above example and put it in the documentation...
from golongpoll.
@jcuga
The awesome golongpoll library indeed :)
Thanks for taking the time to write up the integration code. It looks very doable.
Yes, if you have time, could you put in a new example with this GIN code ?? If not i understand.
Then i can pull it, test, make any PRS.
from golongpoll.
thanks @jcuga i am trying it out now.. cheers
from golongpoll.
Related Issues (20)
- add data backend for scaling and data persistence support HOT 2
- Listen on multiple categories HOT 1
- Do you have an example on how to create a reliable http client to consume golongpoll?
- Offer an "official" Golang client HOT 2
- High CPU load? HOT 5
- client js HOT 3
- Events published in same millisecond aren't seen by clients HOT 3
- Please replace deprecated CloseNotify with Context().Done() HOT 3
- question: version migration guide HOT 2
- How do you make sure the concurrent issue? HOT 2
- Gin framework support HOT 7
- multiple replicas problem HOT 4
- Acks every 15 seconds HOT 1
- hi,how to custom SubscriptionHandler and PublishHandler ?
- Go Mistake #76: time.After and memory leaks
- Handler keeps on waiting for a timeout even though the client has been disconnected. HOT 5
- Latest tag name seems like a typo - messes up SemVer HOT 2
- used in production? HOT 1
- Example of scaling this ? HOT 2
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 golongpoll.