You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importWebSocketfrom'ws';constYOUR_WEBSOCKET_URL='wss://{ID}.execute-api.{REGION}.amazonaws.com/{STAGE}'letsocket=newWebSocket(YOUR_WEBSOCKET_URL);socket.onopen=function(e){console.log("[open] Connection established");console.log("Sending to server");// Ping the serversocket.send(JSON.stringify({type: "ping"}));// Subscribe to a topicsocket.send(JSON.stringify({type: "subscribe",topics: ["topic_one"]}));// Publish a message to a topicsocket.send(JSON.stringify({type: "publish",topic: "topic_one",data: "Hello, subscribers!"}));// Unsubscribe from a topicsocket.send(JSON.stringify({type: "unsubscribe",topics: ["topic_one"]}));};socket.onmessage=function(event){console.log(`[message] Data received from server: ${event.data}`);};socket.onclose=function(event){if(event.wasClean){console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);}else{// e.g. server process killed or network down// event.code is usually 1006 in this caseconsole.log('[close] Connection died');}};socket.onerror=function(error){console.log(`[error] ${error.message}`);};