Git Product home page Git Product logo

sse-go's Introduction

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

sse-go's People

Contributors

dependabot[bot] avatar dipeshdulal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.