Skip to content

Websocket PubSub server application based on gorilla websocket and varto pubsub manager libraries.

Notifications You must be signed in to change notification settings

metinorak/wspubsubserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!',
}));

About

Websocket PubSub server application based on gorilla websocket and varto pubsub manager libraries.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages