Skip to content

Example using Server Sent Events in Go and React.

Notifications You must be signed in to change notification settings

dipeshdulal/sse-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Server Sent Events using go.

Medium Article: https://medium.com/wesionary-team/server-sent-events-with-go-and-react-76df101a3efe

To run server just run: (will run in 5000 port)

go run main.go

To run frontend:

yarn install
yarn start

This will start frontend server in 3000 port.

After running the frontend. You can send request to server or start or stop streaming by clicking in the button show below in screenshot.

Screenshot

What it does.

Client sends the request to /log endpoint and every subscriber that is listening to the stream channel will receive message from server and frontend just displays the messages received from stream.

You can directly check streams going to localhost:5000/sse which will never finish responding to the events until client closes it. After opening the /see you can open another tab to /log and see that tab with /sse has output. Or, if you use curl you can just do.

curl -X GET localhost:5000/log
curl -X POST -d "MY DATA" localhost:5000/log

And check how every request that is made to log endpoint is streamed to sse using ServerSideEvents.

To Initialize SSE from JavaScript
var sse = new EventSource("http://localhost:5000/sse")
sse.onmessage = console.log 
And to close the EventSource.
sse.close()

To learn more about SSE check the mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events