Coder Social home page Coder Social logo

madeindra / golang-websocket Goto Github PK

View Code? Open in Web Editor NEW
37.0 1.0 9.0 128 KB

Pub Sub server via WebSocket using Gorilla WebSocket

Home Page: https://open.madecanggih.dev/websocket

Go 98.59% Makefile 1.41%
go golang chat websocket gorilla gorilla-websocket websockets pubsub

golang-websocket's Introduction

Websocket Server in Go

Running the server

  1. Clone this repository

  2. Mount the repository & run this command to install dependencies

make setup
  1. Run the websocket server
make run
  1. Websocket server will be running on localhost:8080

Using this server with client

  1. After running the server, open your Websocket client. If you don't have any, try Websocket King extension for chrome.

  2. Connect to ws://localhost:8080/socket, you will be greeted by the server.

Server: Welcome! Your ID is f0ab664a-5af3-4f8d-8afe-eb93085267e4
  1. To subscribe to a topic, send this payload (topic can be anything)
{
  "action": "subscribe",
  "topic": "world"
}
  1. To send a message to the topic's subscribers, send payload in this format
{
  "action": "publish",
  "topic": "world",
  "message": "Hello world!"
}
  1. To unsubscribe from the topic, send this payload (topic can be anything)
{
  "action": "unsubscribe",
  "topic": "world"
}

HTTP vs WebSocket

You might be asking "why should I use Websocket instead of REST API"?

REST API uses HTTP which can only send response once per request.

Meanwhile, WebSocket can be used for persistent bidirectional communication without the need of reestablishing connection everytime.

This can be useful in some scenario like chatting or pub-sub.

Here is the diagram to visualize the difference between HTTP and WebSocket.

HTTP-vs-WebSocket

Flowchart

This flowchart describes how this server works. Server-Flowchart

Project Structure

cmd
└── main
    └── main.go
internal
└── websocket
    └── handler.go
    └── model.go
    └── server.go

Main files

main.go: the main file to be executed.

Handler

handler.go: handles open/close connection & pass the message to the server.

Model

model.go: stores the models used by the server.

Server

server.go: runs specific action according to the client message, also containes functions that needed by the server to work properly as a websocket server.

Further Work

This repository is far from ideal. It's just a proof-of-concept.

While this repository is close to a pub-sub, it can still be used for a chat server.

For example, we can add a function on socket connected so that client will be automatically subscribes to their own ID as a topic. Other clients then will use those user's ID as a topic to publish a message.

I have tried building such solution combined with Authorization to prevent other user from subscribing to other's ID and it does work.

Credit

This repository is inspired by Golang-PubSub by @tabvn

golang-websocket's People

Contributors

madeindra avatar

Stargazers

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