Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web Socket connection query support #15

Merged
merged 6 commits into from
Oct 8, 2024
Merged

Web Socket connection query support #15

merged 6 commits into from
Oct 8, 2024

Conversation

Brayden
Copy link
Member

@Brayden Brayden commented Oct 8, 2024

Purpose

Allow clients to query the database from a web socket connection instead of HTTP endpoints. Other changes were required for this to work such as the refactoring of enqueueOperation to return the data, error message and status code instead of a constructed Response object. Sockets need a value returned to them and not a Response.

Tasks

  • Allow users to establish a connection via web sockets
  • Authorize the token is passed in and matches the ENV value
  • Ensure the socket response only is received by the requestor connection
  • Refactor enqueueOperation to respond with data instead of a response

Verify

socket.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket Test</title>
    <style>
        #log {
            white-space: pre-wrap;
            max-height: 200px;
            overflow-y: scroll;
        }
    </style>
</head>
<body>
    <h1>WebSocket Test</h1>
    <div>
        <input type="text" id="messageInput" placeholder="Type a message..." />
        <button onclick="sendMessage()">Send Message</button>
    </div>
    <div id="log"></div>

    <script>
        let socket;
        const log = document.getElementById('log');

        function logMessage(message) {
            log.textContent += message + '\n';
            log.scrollTop = log.scrollHeight;
        }

        function connectWebSocket() {
            logMessage("Connecting to WebSocket...");
            
            socket = new WebSocket('wss://starbasedb.{YOUR-ID-HERE}.dev/socket?token=ABC123');

            socket.onopen = function() {
                logMessage("WebSocket connection opened.");
            };

            socket.onmessage = function(event) {
                logMessage("Received: " + event.data);
            };

            socket.onclose = function(event) {
                logMessage(`WebSocket closed with code: ${event.code}, reason: ${event.reason}, clean: ${event.wasClean}`);
            };

            socket.onerror = function(error) {
                logMessage("WebSocket error: " + error.message);
            };
        }

        function sendMessage() {
            const message = document.getElementById('messageInput').value;
            if (socket && socket.readyState === WebSocket.OPEN) {
                logMessage("Sending: " + message);

                socket.send(JSON.stringify({
                    sql: message, // Submit the messsage with a SQL statement such as "SELECT 1 + 1;"
                    params: [],
                    action: 'query'
                }));
            } else {
                logMessage("WebSocket is not open.");
            }
        }

        window.onload = connectWebSocket;
    </script>
</body>
</html>

Before

After

@Brayden Brayden self-assigned this Oct 8, 2024
@Brayden Brayden added the enhancement New feature or request label Oct 8, 2024
@Brayden Brayden marked this pull request as ready for review October 8, 2024 20:25
@Brayden Brayden merged commit 1958099 into main Oct 8, 2024
@Brayden Brayden deleted the bwilmoth/websocket branch October 8, 2024 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant