Documentation ¶
Overview ¶
This is a basic example of how to use golongpoll.
In this example, we'll generate some random events and provide a way for browsers to subscribe to those events.
To run this example:
go build examples/basic/basic.go
Then run the binary and visit http://127.0.0.1:8081/basic
A couple of notes:
In this example, event payloads are string data, but they can be anything that is convert-able to JSON (passes encoding/json's Marshal() function)
LongpollManager.SubscriptionHandler is directly bound to a http handler. But there's no reason why you can't wrap that handler in your own function to add a layer of validation, access control, or whatever else you can think up. See the advanced example on how to do this.
Our events are simple and randomly generated in a goroutine, but you can create events anywhere and anyhow as long as you pass a reference to the LongpollManager and just call Publish(). Maybe you have a goroutine that checks the weather, a stock price, or something cool. Or you can even have another http handler that calls Publish(). To do this, you must capture the manager reference in a closure. See the advanced example.