Skip to content

@signalwire/realtime-api v4.0.0

Compare
Choose a tag to compare
@Devon-White Devon-White released this 17 Apr 17:26
· 148 commits to main since this release
1ac1bd5

Release of @signalwire/realtime-api v4.0.0

This marks the release of SignalWires new Relay Realtime-api v4 SDK. This SDK strives to model the SDK after the concept of PUC (Programmable Unified Communications.) The v4 SDK will include all the namespaces that were originally included in the v3 SDK:

  • Voice
  • Messaging
  • Chat
  • Video
  • PubSub
  • Task

Setting Up a Single Client in v4

  • In Relay v4, a single client instance provides access to all namespaces, simplifying the development process and reducing code complexity. This unified client architecture makes the system more maintainable and efficient, offering a streamlined approach to accessing different functionalities within the Relay ecosystem.
    This shift in architecture reflects a more modern and developer-friendly approach, focusing on ease of integration and simplicity, which are key in today's fast-paced development environments.

Setting Up a Single Client in v4:

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" });

// Access voice functionalities through the unified client
const voiceClient = client.voice;
const messagingClient = client.messaging;

Advanced Event Listening in Relay v4

Relay v4 introduces a new approach, offering more granular control over applications by allowing
listening to events not only on the Call and RoomSession but also on particular sessions through a new method/parameter called listen.
Examples of some sessions that can be directly listened to are: Collects, Recordings, Playback, Detects, etc.

You can now listen to when this particular session has: Started, Updated, Failed, Ended, and some other session-specific events.

Example:

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })

const voiceClient = client.voice;

// Setup a Voice Client and listen for incoming calls
await voiceClient.listen({
  topics: ["office"],
  onCallReceived: async (call) => {
    call.answer();
    console.log("Call received", call.id);

    // Start a call collect session
    await call.collect({
      digits: {
        max: 4,
        digitTimeout: 10,
        terminators: "#"
      },
      partialResults: true,
      sendStartOfInput: true,
      listen: {
        onStarted: () => {
          console.log("Collect started");
        },
        onInputStarted: (collect) => {
          console.log("Collect input started:", collect.result);
        },
        onUpdated: (collect) => {
          console.log("Collect updated:", collect.result);
        },
        onFailed: (collect) => {
          console.log("Collect failed:", collect.result);
        },
        onEnded: async (collect) => {
          console.log("Collect ended:", collect.result);

          // Play back the digits collected
          await call.playTTS({ text: `You entered ${collect.digits}` });
          call.hangup()
        }
      }
    }).onStarted();
  }
});

Code Examples

Voice

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })


await client.voice.listen({
  topics: ["office"],
  onCallReceived: (call) => {
    call.answer();
    call.playTTS({ text: "Hello world" });
  }
});

Messaging

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here"})

let messageClient = client.messaging;

await messageClient.listen({
  topics: ["office"],
  async onMessageReceived(message) {
    console.log("Received message", message);
    const response = await messageClient.send({
      from: message.to,
      to: message.from,
      body: "Hello World!"
    });
    console.log("Sent message", await response);
  }
})

Chat

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })

const chatClient = client.chat;

await chatClient.listen({
  channels: ["channel1"],
  onMemberJoined: async (member) => {

    let members = await chatClient.getMembers({
      channel: member.channel
    });
    let chatMessage = `Hello ${member.id}!
    There are ${members.members.length} members in this channel.
    The members are: ${members.members.map(m => m.id).join(", ")}`

    await chatClient.publish({
      channel: member.channel,
      content: chatMessage
    })
  }
});

Video

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })

const videoClient = client.video;

await videoClient.listen({
  onRoomStarted: async (room) => {
    console.log("Room started", room.displayName);
  },
  onRoomEnded: async (room) => {
    console.log("Room ended", room.displayName);
  },
});

PubSub

import { SignalWire} from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })

const pubSubClient = client.pubSub;

await pubSubClient.listen({
  channels: ["welcome"],
  onMessageReceived: (message) => {
    console.log("Received message:", message);
  }
});

Task

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "ProjectID Here", token: "Token Here"})

const taskClient = client.task

await taskClient.listen({
  topics: ['office'],
  onTaskReceived: (payload) => {
    console.log('Received task', payload)
  }
});