This test is part of our hiring process for Golang developers. Apply now
This test should take you around one hour. Keep it simple and straightforward.
The goal is to write a simple in-memory PubSub service over WebSocket.
- The service will be a websocket server clients can connect to.
- Clients can subscribe to channels and publish payloads to channels.
- When a payload is published to a channel, all clients subscribed to that channel will receive thepayload.
- Channels and payloads are both arbitrary strings provided by clients.
We only expect you to write the server part of the application, and will provide you with a scaffolding implementation for the client logic and executables.
The server and clients will communicate by sending and receiving websocket text messages containing JSON-encoded data.
{
"type": "<message_type>",
"data": {
<message_data>
}
}
All message types are already defined in pubsub/protocol.go
.
To subscribe to a channel, the client sends a message of type SUBSCRIBE
to the
server.
{
"type": "SUBSCRIBE",
"data": {
"channel": "<channel_name>"
}
}
To publish a payload to a channel, the client sends a message of type PUBLISH
to the server.
{
"type": "PUBLISH",
"data": {
"channel": "<channel_name>",
"payload": "<payload>"
}
}
When a payload is published to a channel, the server sends a message of type
NOTIFY
to all clients subscribed to that channel.
{
"type": "NOTIFY",
"data": {
"channel": "<channel_name>",
"payload": "<payload>"
}
}
Please note the server is not required to log those exact messages, it's just for you to understand the expected behavior.
You only need to implement the websocket server handler. We already provide you with a basic structure for the project, which includes:
- In
cmd/client
andcmd/server
, the executables for the client and server, already implemented. - In
pubsub/client.go
, a basic implementation for the client. You can modify this file if you want but you don't need to to complete the test. - In
pubsub/protocol.go
, the type definitions for the JSON messages exchanged between the client and the server. - In
pubsub/handler.go
, the HTTP handler for the websocket server. This is where you should implement the server logic.
- Correctness: the server should implement the specifications provided above.
- Code quality: your code should be readable and maintainable.
- Thread safety: the server should be able to handle multiple clients concurrently without experiencing race conditions.
- Error handling: the server should handle client disconnections, invalid messages and other error case as gracefully as possible.
Bonus: you can also write tests for your code, but it's not mandatory.
When you feel you are done, open a Pull Request with your change in this repository.