8 releases
0.10.5 | Apr 28, 2024 |
---|---|
0.10.4 | Mar 22, 2024 |
0.10.0 | Feb 26, 2024 |
0.9.6 | Feb 25, 2024 |
#1394 in Web programming
783 downloads per month
72KB
1.5K
SLoC
Megaphone 📣
Megaphone is a reverse proxy that allows clients to connect to a server using long running requests and server streaming. It is useful because it abstracts the complexity of handling long running requests and server streaming from the server, allowing it to focus on the business logic. It also gives the client a single endpoint to connect to, making it easier to manage the connection and reducing the opened connections and the overall traffic overhead.
How it works
Create a channel
To create a channel, the [POST] /create
endpoint must be called.
Each channel:
- has a buffer of 100 messages that will be used to keep messages pending if the consumer is not currently connected. If the buffer is full the server will keep eventual write operations pending for 10 seconds, after that it will respond with a
503 Service Unavailable
status code. - remains alive for 1 minute after the last read operation (or create if no read was performed). After that it is automatically deleted and all buffered messages are lost.
- has two addresses, namely
producerAddress
andconsumerAddress
, the first one can be used to write into the channel, the second one to read from it.
Write into a channel
To write into a channel, the client must call the [POST] /write/{producer-address}/{stream-id}
endpoint.
The server will respond with a 201 Created
status code if the message was successfully written into the channel.
Read from a channel
The only information needed to read from a channel is the consumerAddress
returned when the channel was created.
At the moment the only supported protocol is http streaming, so to read from a channel the client must call the [GET] /read/{consumer-address}
endpoint.
Supported protocols
Http Streaming
To access a channel using http streaming, the client must call the [GET] /read/{consumer-address}
endpoint.
The server will keep the connection open for 20 seconds and send messages as they arrive.
Other repos
- Megaphone Client rust client that can be used to subscribe to megaphone channels.
- Megaphone Client JS Javascript/Typescript client that can be used to subscribe to megaphone channels.
- Megaphone Operator Megaphone kubernetes operator
- Megaphone Demo Demo application that uses megaphone channels to implement a chat.
Dependencies
~23–38MB
~666K SLoC