BETA - Websockets are live and production-ready, but have not been tested for extensive lengths of time. Make sure to have fall back solutions in place before switching.

We recommend you get familiar with normal HTTP requests before using Websockets.

Behaviour

The Websockets endpoint is minimally interactive, you should expect no need to handle extra logic on top of normal HTTP requests. Authentication failures are communicated at connection time, and the connection is terminated immediately. We do not send FIN packets, so plan accordingly (do not use end event handlers, use close).

If the connection was not closed promptly, you may start sending messages over the channel immediately. Due to the nature of websockets, you should plan on asynchronous handling (including no-message-order guarantees).

PropertyValue
URI Stringwss://api.chatsight.ai:443/text/moderation
Headersx-chatsight-api-auth
JSON Payload{ "sample":"hello world", "reference": "comment-1234" }
JSON ResponseSee Sending a Moderation Request

Reference

Below is a semi-production ready implementation using the websocket NodeJS Library. You should adapt the code to fit your use case, and consider using a class as described in the comments set out below.

const main = async () => {
    var WebSocketClient = require('websocket').client;
    var client = new WebSocketClient();
    var tryRender = async message => {
        try{
            var parse = JSON.parse(message)
            return parse
        }catch(err){
            return false
        }
    }
    client.on('connect', async connection => {
        connection.on('message', async message => {
            if(message.type === 'utf8') {
                if(typeof message.utf8Data !== 'string') return;
                var render = await tryRender(message.utf8Data)
                if(render === false) return;
                var reference = render.response.metadata.self_reference
                // Your custom logic, using reference to find the content, and then passing the typical moderation response.
                await my_custom_callback_logic({comment_id: reference, data: render})

            }
        });
        /*
            To make a request, you simply pass a normal JSON payload. 
            In production, we recommend you make this into a class, 
            and assign the connection reference to the classes property 
            so you can call it outside of this event listener.

            i.e.
            
            class my_class {
                constructor(){
                    this.send_moderation_request = null
                }
                async startconnection(){
                    client.on('connect', async connection => {

                        ... the code in this example ...

                        this.send_moderation_request = connection.sendUTF
                    })
                }

                async moderate_comment(text,reference){
                    await this.send_moderation_request(JSON.stringify({"sample":text,"reference":reference}))
                }

            }

        */
        connection.sendUTF(
            JSON.stringify(
                {
                    "sample":`hello world`,
                    "reference": "" /* UUID, Timestamp, Username-Timestamp, Anything - use this field to identify an async response. Pretty much required for websockets to make sense. */
                }
            )
        )
        connection.on('close', function() {
            // Custom Logic to handle web sockets being closed, and handling auth failures.
        });
    })
    client.connect(
        'wss://api.chatsight.ai:443/text/moderation', 
        'echo-protocol',
        null,
        {
            "x-chatsight-api-auth":"c.xyz.abc123"
        },
        null
    )
}
main().catch(k => {
    console.log(k)
})