2.11.2 • Published 7 months ago

@relaybox/client v2.11.2

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

npm version

@relaybox/client

Find the full technical documention here

Welcome to RelayBox.

In order to use this library, you need to create a free account and API key. Find more details here.

If you find any issues, please report them here or contact support@relaybox.net.

Installation

In order to connect to RelayBox you first need to install the client library SDK. The library is made available via the NPM registry.

npm install @relaybox/client

Connecting an Application

Once the installation is complete, you'll be able to access the service by initializing a new RelayBox instance.

import RelayBox from '@relaybox/client';

const relayBox = new RelayBox({
  apiKey: xxxx.xxxx.xxxxxxxx // Replace with your api key
});

await relayBox.connect();

Create and Join a Room

Rooms are logical groups of connections that enable communication via events. To create a room, call the join() method passing the name of the room as an argument.

const myFirstRoom = await relayBox.join('myFirstRoom');

If the room does not already exist, joining will create it.

Subscribing to Events

Events are data packets transmitted to other connections listening for them (subscribers). To subscribe to an event, provide an event name and a handler function to process data as it arrives.

await myFirstRoom.subscribe('message', (data) => {
  console.log(data);
});

Above, a subscription to the "message" event has been registered with a corresponsing callback function to handle the data. In our case, we will simply log the data in the console.

Publishing an Event

To publish an event, call the publish() method, providing an event name and the data to transmit. Since we're already subscribed, let's publish a "message" event...

const response = await myFirstRoom.publish('message', { hello: 'universe' });

Putting It All Together

Putting that all together, we can see the overall structure of the code.

import RelayBox from '@relaybox/client';

const relayBox = new RelayBox({
  apiKey: xxxx.xxxx.xxxxxxxx // Replace with your api key
});

await relayBox.connect();

const myFirstRoom = await relayBox.join('myFirstRoom');

await myFirstRoom.subscribe('message', (data) => {
  console.log(data);
});

const response = await myFirstRoom.publish('message', { hello: 'universe' });

Here, we've established a connection and joined a room. We've subscribed to an event and published an event. All connections in the room subscribed to either this specific event or to all room events will receive this data in real-time.

Note: Always be cautious, never expose production API keys!

This guide uses an API key directly in the client-side code. While this approach is possible and the quickest way to connect, it should be used with caution. The recommended, secure approach is to generate auth tokens using a designated endpoint.

License

This project is licensed under the MIT License - see the LICENSE file for details.

2.11.0

7 months ago

2.11.1

7 months ago

2.11.2

7 months ago

2.10.0

8 months ago

2.9.0

8 months ago

2.8.0

8 months ago

2.9.1

8 months ago

2.3.1

9 months ago

2.7.0

8 months ago

2.3.4

9 months ago

2.6.0

9 months ago

2.3.3

9 months ago

2.3.5

9 months ago

3.0.0

9 months ago

2.2.0

9 months ago

2.1.0

9 months ago

2.0.0

9 months ago

1.2.1

10 months ago

1.2.0

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago