Skip to content

Latest commit

 

History

History
53 lines (46 loc) · 1.61 KB

README.md

File metadata and controls

53 lines (46 loc) · 1.61 KB

Websocket PubSub server written in Go

  • This project uses gorilla/websocket to implement a simple PubSub server. And for managing the connections, subscriptions and publishing messages, it uses metinorak/varto package.

  • It wraps gorilla WebSocket connection into a struct and implements the varto.Connection interface. So it can be used as a connection type for varto pubsub manager.

Usage

  • To run the server, you need to have Go installed on your machine. Then you can run the following command to start the server:
go run main.go
  • You can specify the port number to run the server on by setting the PORT environment variable.
  • To connect to the server, you can use any websocket client.

Example

  • You can use the following code to connect to the server and subscribe to a topic:
const ws = new WebSocket('ws://localhost:8080/ws');
ws.onopen = () => {
  ws.send(JSON.stringify({
    action: 'SUBSCRIBE',
    topic: 'test',
  }));
};
ws.onmessage = (msg) => {
  console.log(msg);
};
  • You can publish a message to a topic by sending a message to the server:
ws.send(JSON.stringify({
  action: 'PUBLISH',
  topic: 'test',
  message: 'Hello World!',
}));
  • You can unsubscribe from a topic by sending a message to the server:
ws.send(JSON.stringify({
  action: 'UNSUBSCRIBE',
  topic: 'test',
}));
  • You can broadcast a message to all the clients by sending a message to the server:
ws.send(JSON.stringify({
  action: 'BROADCASTALL',
  message: 'Hello World!',
}));