Skip to content

Sending Realtime Data

Once the realtime connection is established, users can start sending realtime data to another user.

Sending Data to a User

User can send any JSON data over to any users by using postRealtime() method.

For more detailed information on all the parameters and options available with the postRealtime() method, please refer to the API Reference below:

postRealtime(message, recipient): Promise<{ type: 'success', message: string }>

WARNING

The receiver must also be connected to the realtime connection to receive the data.

html
<form onsubmit="skapi.postRealtime(event, 'recipient_user_id').then(u=>console.log(u))">
    <input name="msg" required><input type="submit" value="Send">
</form>
js
skapi.postRealtime({ msg: "Hello World!" }, 'recipient_user_id').then(res => console.log(res));

Example above shows how to send realtime data to a user with an id: 'recipient_user_id' (Should be a real user_id that you see in user profiles).

postRealtime() method can be used directly from the form element as shown in the example above. postRealtime() takes two arguments:

  • message: The data to be sent to the recipient. It can be any JSON parsable data, or a SubmitEvent object.
  • recipient: The user ID of the recipient or the name of the group the user have joined.

When the message is sent successfully, the method will return the following object:

ts
{
  type: 'success',
  message: 'Message sent.'
}

On the receiver's side, the message will be received as an argument as an object with type and message properties through the RealtimeCallback that has been set when creating the realtime connection via connectRealtime() method.