*** id: 19dbd10c-0d44-4537-b694-2387cbe71cce title: Relay.Client slug: /node/reference/relay-client max-toc-depth: 3 ---------------- [link-1]: # [link]: #events [relay-calling]: /docs/server-sdk/v2/node/reference/calling [relay-messaging]: /docs/server-sdk/v2/node/reference/messaging `Relay.Client` is the basic connection to RELAY, allowing you send commands to RELAY and setup handlers for inbound events. ## Constructor Constructs a client object to interact with RELAY. ### Parameters | Parameter | Type | Default | Description | | --------- | -------- | ------------ | ------------------------------------- | | `project` | `string` | **required** | Project ID from your SignalWire Space | | `token` | `string` | **required** | Token from your SignalWire Space | ### Example Create a Client to interact with the RELAY API. ```javascript const client = new RelayClient({ project: 'my-project-id', token: 'my-project-token' }) ``` ## Properties | Property | Type | Description | | ----------- | ------------------------------------ | --------------------------------------------------------------- | | `connected` | `boolean` | Returns *true* if the client has connected to Relay | | `calling` | [`Relay.Calling`][relay-calling] | Returns a `Relay.Calling` instance associated with the client | | `messaging` | [`Relay.Messaging`][relay-messaging] | Returns a `Relay.Messaging` instance associated with the client | ## Methods ### connect Activates the connection to the RELAY API. The connection to RELAY does not happen automatically so that you can setup handlers to events that might occur before the connection is successfully established. #### Returns `Promise` #### Example ```javascript // Make sure you have attached the listeners you need before connecting the client, or you might miss some events. await client.connect() ``` ### disconnect Disconnect the client from RELAY. #### Returns `void` #### Example ```javascript client.disconnect() ``` ### on Attach an event handler for a specific type of event. #### Parameters | Parameter | Type | Default | Description | | --------- | ---------- | ------------ | ----------------------------------------------------------- | | `event` | `string` | **required** | Event name. Full list of events [Relay.Client Events][link] | | `handler` | `function` | **required** | Function to call when the event comes | #### Returns [`Relay.Client`][link-1] - The client object itself. #### Example Subscribe to the `signalwire.ready` and `signalwire.error` events. ```javascript client.on('signalwire.ready', (client) => { // Your client is ready! }).on('signalwire.error', (error) => { // Got an error... }) ``` ### off Remove an event handler that were attached with `.on()`. If no `handler` parameter is passed, all listeners for that `event` will be removed. #### Parameters | Parameter | Type | Default | Description | | --------- | ---------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `event` | `string` | **required** | Event name. Full list of events [Relay.Client Events][link] | | `handler` | `function` | **optional** | Function to remove.
*Note: `handler` will be removed from the stack by reference so make sure to use the same reference in both `.on()` and `.off()` methods.* | #### Returns [`Relay.Client`][link-1] - The client object itself. #### Example Subscribe to the `signalwire.error` and then, remove the event handler. ```javascript const errorHandler = (error) => { // Log the error.. } client.on('signalwire.error', errorHandler) // .. later client.off('signalwire.error', errorHandler) ``` ## Events All available events you can attach a listener on. | Event | Description | | --------------------------- | ----------------------------------------------------------------------- | | `signalwire.ready` | The session has been established and all other methods can now be used. | | `signalwire.error` | There is an error dispatch at the session level. | | `signalwire.socket.open` | The websocket is open. However, you have not yet been authenticated. | | `signalwire.socket.error` | The websocket gave an error. | | `signalwire.socket.message` | The client has received a message from the websocket. | | `signalwire.socket.close` | The websocket is closing. |