SocketBee JavaScript
The SocketBeeClient
is a JavaScript client library for interacting with the SocketBee service. It leverages socket.io-client
to manage WebSocket connections and facilitate real-time event handling. This guide will cover the installation, setup, and usage of the SocketBeeClient
.
Installation
To use SocketBeeClient
, you need to install the package.
sh
npm install @socketbee/client
Initialization
To initialize the SocketBeeClient
, you need to import the package and create an instance by passing the required parameters.
javascript
import SocketBeeClient from '@socketbee/client';
const app_id = 'your_app_id';
const app_key = 'your_app_key';
const options = {
secret: 'your_app_secret',
server: 'custom_server', // Optional
port: 443, // Optional
region: 'your_region', // Optional
protocol: 'https' // Optional
};
const socketClient = new SocketBeeClient(app_id, app_key, options);
Parameters
app_id
(string): Your application ID.app_key
(string): Your application key.options
(object): An optional configuration object.secret
(string): Your application secret.server
(string): The server address (default is 'east-1.socket.socketbee.com').port
(number): The server port (default is 443).region
(string): The region of the server.protocol
(string): The protocol to use (default is 'https').client_events
(string): Allow to send events from the client (default is false).
Methods
subscribe(channel, callback)
Subscribes to a specific channel and initializes the WebSocket connection.
Parameters
channel
(string): The name of the channel to subscribe to.callback
(function): A callback function that is executed when the client joins the channel.
Usage
javascript
socketClient.subscribe('my_channel', () => {
console.log('Successfully joined the channel');
});
listen(event, callback)
Listens for events on the subscribed channel.
Parameters
event
(string): The name of the event to listen for.callback
(function): A callback function that is executed when the event is received.
Usage
javascript
socketClient.listen('message', (data) => {
console.log('Received message:', data);
});
broadcast(event, callback)
Broadcasts an event to the subscribed channel.
Parameters
event
(object): An object containing event details.event.channel
(string): The channel name (optional if already subscribed to a channel).event.event
(string): The event name.event.data
(any): The data to broadcast with the event.
callback
(function): A callback function that is executed after broadcasting the event.
Usage
javascript
socketClient.broadcast({
event: 'new_message',
data: { text: 'Hello, world!' }
}, () => {
console.log('Message broadcasted');
});
Examples
Example 1: Basic Subscription and Listening
javascript
import SocketBeeClient from '@socketbee/client';
const app_id = 'your_app_id';
const app_key = 'your_app_key';
const socketClient = new SocketBeeClient(app_id, app_key);
socketClient.subscribe('my_channel', () => {
console.log('Successfully joined the channel');
});
socketClient.listen('message', (data) => {
console.log('Received message:', data);
});
Example 2: Broadcasting an Event
javascript
import SocketBeeClient from '@socketbee/client';
const app_id = 'your_app_id';
const app_key = 'your_app_key';
const options = {client_events: true}
const socketClient = new SocketBeeClient(app_id, app_key, options);
socketClient.subscribe('my_channel', () => {
console.log('Successfully joined the channel');
});
socketClient.broadcast({
event: 'new_message',
data: { text: 'Hello, world!' }
}, () => {
console.log('Message broadcasted');
});
Example 3: Using Options
javascript
import SocketBeeClient from '@socketbee/client';
const app_id = 'your_app_id';
const app_key = 'your_app_key';
const options = {
secret: 'your_app_secret',
server: 'custom_server',
port: 443,
region: 'your_region',
protocol: 'https'
};
const socketClient = new SocketBeeClient(app_id, app_key, options);
socketClient.subscribe('my_channel', () => {
console.log('Successfully joined the channel');
});
socketClient.listen('message', (data) => {
console.log('Received message:', data);
});
socketClient.broadcast({
event: 'new_message',
data: { text: 'Hello, world!' }
}, () => {
console.log('Message broadcasted');
});