Last updated
Was this helpful?
Last updated
Was this helpful?
In this article, we will provide examples in Python to demonstrate how the client application interacts with the PortSIP PBX IM service. For these examples, we assume the PBX server is hosted at https://pbx.portsip.com:8887
.
The client application needs to use WebSocket to connect to the URL wss://pbx.portsip.com:8887/im
.
Once the connection to the IM server is successfully established, the client must authenticate with the server in order to send and receive messages.
The IM service authenticates the client application using the PortSIP PBX access token. Please follow the instructions in the article "" to obtain the token.
Once the access token is obtained, you can authenticate with the IM service by sending the login
command to the server.
If authentication is successful, the server will respond as shown below.
["ctrl"]["params"]["user"]
: Represents the user ID.
["ctrl"]["params"]["token"]
: Represents the user's authentication token, which can be used for verifying permissions in future HTTP file uploads to the IM server.
P2P topics represent a communication channel between two users, and these topics do not have an owner.
The topic IDs for the two participating users are different. Each user views the other user’s ID as the P2P topic ID. For example, if the two users in the topic are usr804252613548703744
and usr804252613548777777
, the first user will see the topic ID as usr804252613548777777
, while the second user will see the topic ID as usr804252613548703744
.
You can send a message to a P2P topic for another user using the following code snippet. The ["pub"]["id"]
is a UUID that the client application must generate. The server will return this ID in its response, allowing the client to track the message by this ID.
The response will look like the following:
The user with the ID 804252613548777777
will receive the message as shown below:
You can use the following code to retrieve the topics you have subscribed to.
The response will look like the following:
You can use the following message {get what="desc"}
to query the details of a topic. For example, group-related information can be found within the details of a group topic.
The response will look like the following:
By using the ["meta"]["desc"]["seq"]
field returned from the {get what="desc"}
message, you can retrieve the number of messages in the subscribed topic. You can then use {get what="data"}
to fetch historical messages.
You can use the following message to create a group chat.
The response will look like the following, the ["ctrl"]["topic"]
represents the group topic. Then other users can subscribe to group messages by sending {sub topic="grpf0NlkdDEQHs"}
.
The user can send the following message to subscribe to the group topic, allowing them to join the group and send or receive messages within the group.
The response will look like the following:
You can use the following code to send a message to a group.
The response will look like the following if succeeds:
The following message will be sent to all users who have subscribed to the group topic.
Users can unsubscribe from a group by sending a {leave}
message. This is the opposite function of the {sub}
message. The {leave}
message supports two options:
Leave without unsubscribing (unsub=false
)
Unsubscribe and leave (unsub=true
)
The server will respond to a {leave}
message with a {ctrl}
packet.
Leave without unsubscribing (unsub=false
) only affects the current WebSocket session. You will still receive messages if you establish a new WebSocket session with the IM server, and if the user is connected to the IM server from another app, they will also continue receiving messages.
Unsubscribe and leave (unsub=true
) affects all sessions for that user. The user will no longer receive messages from this group across any session unless he subscribes to this group topic again.
The response will look like the following if succeeds:
The group administrator can remove a member from the group by sending the following message:
The response will look like the following if succeeds:
The group administrator can delete a group by sending the following message:
The response will look like the following if succeeds:
The response will look like the following if succeeds:
The group administrator can transfer ownership of the group to another user by sending the following message. In this example, usr804252613548777777
indicates that the group ownership will be transferred to the user with the ID 804252613548777777
.
The responses will look like the following if succeed:
To send a file in a chat, you first need to upload the file to the IM server using an HTTP POST form request to https://pbx.portsip.com:8887/im/file
. An example code is provided below:
The response will look like the following if succeeds:
["ctrl"]["params"]["url"]
is the file download URL. The sender can send this URL in a message to the recipient. After receiving the message, the recipient can download the file and display it in the chat according to the file type.
Once a user receives a message containing the file URL, they can assemble the URL as shown below and download the file using an HTTP GET request.
The group administrator can update the group information by sending the following message. The information structure is defined in the of the group topic.