Skip to content

SocketBee Python SDK

The socketbee library is a Python package that allows you to send real-time events to clients connected to SocketBee. It provides a simple interface for sending events to specific channels, making it easy to integrate real-time functionality into your Python applications.

Installation

You can install the socketbee library using pip:

bash
pip install socketbee

Usage

Here's how you can use the socketbee library in your Python code:

  1. Import the library
python
from socketbee import SocketBee
  1. Initialize the SocketBee client

To initialize the SocketBee client, you'll need to provide your app ID, secret key, and key. These credentials can be obtained from your SocketBee account dashboard.

python
client = SocketBee(app_id='YOUR_APP_ID', secret='YOUR_SECRET', key='YOUR_KEY')

You can also provide additional configuration options by passing a config dictionary to the SocketBee constructor. The available configuration options are:

  • protocol (default: 'https'): The protocol to use for the SocketBee API endpoint.
  • host (default: 'east-1.socket.socketbee.com'): The hostname of the SocketBee API endpoint.
  • port (default: 443): The port number of the SocketBee API endpoint.

Example with custom configuration:

python
config = {
    'protocol': 'http',
    'host': 'example.socketbee.com',
    'port': 8080,
}
client = SocketBee(app_id='YOUR_APP_ID', secret='YOUR_SECRET', key='YOUR_KEY', config=config)
  1. Send an event

To send an event, use the send_event method of the SocketBee client. This method takes three arguments:

  • channel: The name of the channel to which the event should be sent.
  • event: The name of the event.
  • data: The data payload for the event. This can be any JSON-serializable data structure (e.g., a dictionary, list, string, number).

Example:

python
response = client.send_event(channel='my-channel', event='new-message', data={'message': 'Hello, world!'})

The send_event method returns a dictionary with the following keys:

  • status: The HTTP status code of the response.
  • body: The response body, if the request was successful.
  • error_message: An error message, if an exception occurred during the request.

Example of handling the response:

python
response = client.send_event(channel='my-channel', event='new-message', data={'message': 'Hello, world!'})

if response['status'] == 200:
    print('Event sent successfully!')
    print(response['body'])
else:
    print(f'Error: {response.get("error_message", response.get("errors"))}')

Examples

Here's an example of how you might use the socketbee library in a Flask application to send real-time notifications:

python
from flask import Flask, request
from socketbee import SocketBee

app = Flask(__name__)
client = SocketBee(app_id='YOUR_APP_ID', secret='YOUR_SECRET', key='YOUR_KEY')

@app.route('/send-notification', methods=['POST'])
def send_notification():
    data = request.get_json()
    response = client.send_event(channel='notifications', event='new-notification', data=data)
    return response

if __name__ == '__main__':
    app.run()

In this example, when a POST request is made to the /send-notification endpoint with a JSON payload, the application sends a real-time event to the notifications channel using the socketbee library.

Conclusion

The socketbee library provides a simple and efficient way to integrate real-time functionality into your Python applications using SocketBee. With its straightforward API and comprehensive documentation, you can easily send events to connected clients and build interactive, real-time experiences.