Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SocketClient

[ALPHA - API will 100% change in the near future. Don't say I didn't warn you.]

An easy to use socket implementation that allows users to connect into remote instances of the EASY API.

How to use it:

  1. Make sure you're running an instance of the EASY API and make sure to start it with the --socket flag

        > docker run -e PORT=8080 -p 8080:8080 openwa/wa-automate:latest --socket
    
  2. Use this in your code:

        import { SocketClient } from "@open-wa/wa-automate";
        
        SocketClient.connect("http://localhost:8080").then(async client => {
            //now you can use the client similar to how you would use the http express middleware.
    
            //There are two main commands from this client
    
            // 1. client.listen - use this for your listeners
            
            await client.listen("onMessage", message => {
                ...
            })
    
            // 2. client.asj - ask the main host client to get things done
    
            await client.ask("sendText", {
                "to" : "44771234567@c.us",
                "content": "hellow socket"
            })
    
            // or you can send the arguments in order as an array (or tuple, as the cool kids would say)
            await client.ask("sendText", [
                "44771234567@c.us",
                "hellow socket"
            ])
    
        })
    

Hierarchy

  • SocketClient

Index

Constructors

constructor

  • new SocketClient(url: string, apiKey?: string, ev?: boolean): SocketClient
  • Parameters

    • url: string
    • Optional apiKey: string
    • Optional ev: boolean

    Returns SocketClient

Properties

apiKey

apiKey: string

ev

ev: EventEmitter2

A local version of the ev EventEmitter2

listeners

listeners: { onAck: any; onAddedToGroup: any; onAnyMessage: any; onBattery: any; onBroadcast: any; onButton: any; onChatDeleted: any; onChatOpened: any; onChatState: any; onContactAdded: any; onGlobalParticipantsChanged: any; onIncomingCall: any; onLabel: any; onLogout: any; onMessage: any; onMessageDeleted: any; onNewProduct: any; onOrder: any; onPlugged: any; onReaction: any; onRemovedFromGroup: any; onStateChanged: any; onStory: any } = {}

Type declaration

  • onAck:
  • onAddedToGroup:
  • onAnyMessage:
  • onBattery:
  • onBroadcast:
  • onButton:
  • onChatDeleted:
  • onChatOpened:
  • onChatState:
  • onContactAdded:
  • onGlobalParticipantsChanged:
  • onIncomingCall:
  • onLabel:
  • onLogout:
  • onMessage:
  • onMessageDeleted:
  • onNewProduct:
  • onOrder:
  • onPlugged:
  • onReaction:
  • onRemovedFromGroup:
  • onStateChanged:
  • onStory:

socket

socket: Socket<DefaultEventsMap, DefaultEventsMap>

url

url: string

Methods

ask

  • ask<M, P>(method: M, args?: any[] | P | {}): Promise<unknown>

createMessageCollector

listen

  • listen(listener: SimpleListener, callback: (data: unknown) => void): Promise<string>
  • Set a callback on a simple listener

    Parameters

    • listener: SimpleListener

      The listener name (e.g onMessage, onAnyMessage, etc.)

    • callback: (data: unknown) => void

      The callback you need to run on the selected listener

        • (data: unknown): void
        • Parameters

          • data: unknown

          Returns void

    Returns Promise<string>

    The id of the callback

stopListener

  • Discard a callback

    Parameters

    • listener: SimpleListener

      The listener name (e.g onMessage, onAnyMessage, etc.)

    • callbackId: string

      The ID from listen

    Returns boolean

    boolean - true if the callback was found and discarded, false if the callback is not found

Static connect

  • The main way to create the socket based client.

    Parameters

    • url: string

      URL of the socket server (i.e the EASY API instance address)

    • Optional apiKey: string

      optional api key if set

    • Optional ev: boolean

    Returns Promise<SocketClient & Client>

    SocketClient