Skip to end of metadata
Go to start of metadata

Overview

Since build 1.1.0.45 it is possible to send a message with JSON data from publishing client to all the subscribers of stream published, and receive this message on playing client.

Sending a message

To send a message, Stream.sendData() method is used. The message data should be a serialized valid JSON object

code

        mSendDataButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (publishStream == null) {
                    return;
                }
                ...
                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, Object> data = null;
                try {
                    data = objectMapper.readValue(dataStr, HashMap.class);
                } catch (IOException e) {
                    Log.w(TAG, "Not valid json string");
                    ...
                    return;
                }
                publishStream.sendData(data);
            }
        });

Receiving a message

To receive a message, check in onStreamEvent handler: if the event has treamEventType type, then message contains a serialized JSON data to parse

code

    playStream.on(new StreamEventHandler() {
        ...
        @Override
        public void onStreamEvent(StreamEvent streamEvent) {
            if (StreamEventType.data.equals(streamEvent.getType())) {
                parseReceivedData(streamEvent.getPayload());
            }
        }
    });
    ...
    private void parseReceivedData(Map<String, Object> data) {
        runOnUiThread(() -> {
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
            StringBuilder stringBuilder = new StringBuilder(formatter.format(date) + " - ");
            for (Map.Entry<String, Object> obj : data.entrySet()) {
                stringBuilder.append(obj.getKey()).append("=").append(obj.getValue()).append(", ");
            }
            stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());

            mReceivedDataTextView.setText(stringBuilder);
        });
    }

A message sent from Web SDK or using REST API may be received by this way

  • No labels