1

I have integrated zulip api using js and ajax , the problem is that the exchanged messages are not in real tile so pleasen, how to listen to real time event zulip using js and ajax, here is my js code

$("#post").click(function () {

var url = 'https://domain_name/api/v1/messages';
var content = $("#postButton").val();
$.ajax({
    type: "post",
    url: url,
    data: {type: 'private', content: content, to: 'khalil@gmail.com'},
    headers: {Authorization: getBasicAuthenticationToken(username, 
    password)},
   

   })






  });


$(function () {
var $msgs = $('#msgs');


 $.ajax({
    type: 'GET',
    url: 'https://domain_name/api/v1/messages',
    data: {num_before: 10, num_after: 1000, use_first_unread_anchor: true},
    headers: {
        "Authorization": getBasicAuthenticationToken(username, password)
    },
    success: function (msgs) {
   
        /*console.log(msgs['messages'][0]['sender_email']);*/
        $.each(msgs.messages, function (i, msg) {
            $msgs.append('<li>' + msg.content + '</li>');
            $msgs.append('<li>' + msg.sender_email + '</li>');
            var who = msg.sender_email;

            insertChat('<p>' + who + '</p>', '<p>' + msg.content +'</p>',0)
        });
    }
});
});
Anders Kaseorg
  • 3,657
  • 22
  • 35
user1111
  • 25
  • 5

1 Answers1

2

The API you need is the Zulip real-time events API. First, register an event queue with GET /api/v1/register, specifying event_types=["message"]. Then you can get events from the queue by repeatedly long-polling GET /api/v1/events.

Anders Kaseorg
  • 3,657
  • 22
  • 35