GithubHelp home page GithubHelp logo

Comments (5)

jcuga avatar jcuga commented on June 23, 2024

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.

jcuga avatar jcuga commented on June 23, 2024

And yes, to add some simple notifications to a webapp avoiding the extreme overkill that is websockets is a good call.

from golongpoll.

jcuga avatar jcuga commented on June 23, 2024

If this works out, I should probably clean up my above example and put it in the documentation...

from golongpoll.

joeblew99 avatar joeblew99 commented on June 23, 2024

@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.

joeblew99 avatar joeblew99 commented on June 23, 2024

thanks @jcuga i am trying it out now.. cheers

from golongpoll.

Related Issues (20)

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.