Modules@robinpath/socket
socket

@robinpath/socket

0.1.4Node.jsPublic

WebSocket client for real-time communication

Socket

WebSocket client for real-time communication with message history, handlers, and connection management

Package: @robinpath/socket | Category: Web | Type: Utility

Authentication

socket.connect "slack" "wss://wss.slack.com/link"

Call this once at the start of your script before using any other function. Credentials persist for the duration of the script execution.

Use Cases

Use the socket module when you need to:

  • Send a message through a WebSocket connection -- Use socket.send to perform this operation
  • Wait for and receive the next message -- Use socket.receive to perform this operation
  • Get recent message history -- Use socket.messages to perform this operation
  • Check if a WebSocket is connected -- Use socket.isConnected to perform this operation
  • Close a WebSocket connection -- Use socket.close to perform this operation

Quick Reference

FunctionDescriptionReturns
connectConnect to a WebSocket server{name, url, connected}
sendSend a message through a WebSocket connectionTrue if sent
receiveWait for and receive the next messageReceived message
messagesGet recent message historyArray of recent messages
isConnectedCheck if a WebSocket is connectedTrue if open
closeClose a WebSocket connectionTrue if closed
onMessageRegister a handler for incoming messagesTrue
pingSend a ping to keep the connection aliveTrue if sent

Functions

connect

Connect to a WebSocket server

Module: socket | Returns: object -- {name, url, connected}

socket.connect "slack" "wss://wss.slack.com/link"
ParameterTypeRequiredDescription
namestringYesConnection name
urlstringYesWebSocket URL (ws:// or wss://)
optionsobjectNo{headers, timeout, maxHistory}

send

Send a message through a WebSocket connection

Module: socket | Returns: boolean -- True if sent

socket.send "slack" {"type": "message", "text": "Hello"}
ParameterTypeRequiredDescription
namestringYesConnection name
dataanyYesMessage (string or JSON)

receive

Wait for and receive the next message

Module: socket | Returns: any -- Received message

socket.receive "slack" 5000
ParameterTypeRequiredDescription
namestringYesConnection name
timeoutnumberNoTimeout in ms (default 10000)

messages

Get recent message history

Module: socket | Returns: array -- Array of recent messages

socket.messages "slack" 10
ParameterTypeRequiredDescription
namestringYesConnection name
limitnumberNoMax messages (default 50)

isConnected

Check if a WebSocket is connected

Module: socket | Returns: boolean -- True if open

socket.isConnected "slack"
ParameterTypeRequiredDescription
namestringYesConnection name

close

Close a WebSocket connection

Module: socket | Returns: boolean -- True if closed

socket.close "slack"
ParameterTypeRequiredDescription
namestringYesConnection name

onMessage

Register a handler for incoming messages

Module: socket | Returns: boolean -- True

socket.onMessage "slack" $handler
ParameterTypeRequiredDescription
namestringYesConnection name
handlerstringYesCallback function

ping

Send a ping to keep the connection alive

Module: socket | Returns: boolean -- True if sent

socket.ping "slack"
ParameterTypeRequiredDescription
namestringYesConnection name

Error Handling

All functions throw on failure. Common errors:

ErrorCause
WebSocket URL is requiredCheck the error message for details
Handler must be a functionCheck the error message for details
Connection "..." not foundCheck the error message for details
Connection "..." is not openCheck the error message for details
@desc "Send and validate result"
do
  set $result as socket.send "slack" {"type": "message", "text": "Hello"}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Create a new item with send

Create a new resource and capture the result.

set $result as socket.send "slack" {"type": "message", "text": "Hello"}
print "Created: " + $result

2. Multi-step Socket workflow

Chain multiple socket operations together.

@desc "Connect, send, and more"
do
  set $r_connect as socket.connect "slack" "wss://wss.slack.com/link"
  set $r_send as socket.send "slack" {"type": "message", "text": "Hello"}
  set $r_receive as socket.receive "slack" 5000
  print "All operations complete"
enddo

3. Safe connect with validation

Check results before proceeding.

@desc "Connect and validate result"
do
  set $result as socket.connect "slack" "wss://wss.slack.com/link"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.4latest1 months ago
Install
$ robinpath add @robinpath/socket

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.4
LicenseMIT
Unpacked Size7.0 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

web