How to create a WebSocket server

Originally Published on 06/23/2016 | Updated on 03/13/2017 8:11 am PDT

With Wowza Streaming 4.5.0 and later, you can use an HTTP Provider to host a WebSocket communication session. HTTP Providers are Java classes that are configured on a per-virtual host basis. They are lightweight web applications that can be used to send information to or obtain information from a Wowza Streaming Engine instance. This article describes how to create an HTTP Provider to host a WebSocket session.

A WebSocket session provides a persistent connection between a browser and Wowza Streaming Engine. The WebSocket protocol can be used to send text or binary data between browsers. Examples of WebSocket applications are text, chat, out-of-band metadata, and control data.

Contents

Introduction

HTTP Providers are mini HTTP web applications that can be used to extend Wowza media server software functionality. They are configured on a per-port basis in [install-dir]/conf/VHost.xml. An individual HTTP Provider can be protected by a user name and password. Multiple HTTP Providers can be attached to a single port and a specific HTTP Provider can be selected based on a request filter. HTTP Providers can also host WebSocket sessions for sending asynchronous, bi-directional information to and from a Wowza Streaming Engine instance.

The WebSocket protocol provides full-duplex communication changes over a TCP connection. The WebSocket protocol is supported by most modern web browsers and can be used with socket servers such as node.js. A WebSocket connection starts as an HTTP upgrade request. In your HTTP Provider, you can accept the upgrade request and a WebSocket session is created and used to send and receive messages.

This HTTP Provider echos back the WebSocket messages it receives, and will periodically broadcast a text WebSocket message to all connected sessions.

Sending and receiving WebSocket messages

WebSocket messages are received by a listener class that implements the IWebSocketEventNotify interface. We recommend that you create a class that extends the WebSocketEventNotifyBase and overrides the callback methods you want to use.

There are two types of WebSocket messages: binary and text.

A text WebSocket message is created using the following API:

WebSocketMessage.createMessageText(boolean mask, String messageStr);

A binary Websocket message is created using one of the following APIs:

A WebSocket message can be sent over an individual WebSocketSession using the following API:

webSocketSession.sendMessage(WebSocketMessage message);

An HTTP Provider maintains a list of active WebSocket sessions. A message can be broadcast to all sessions of a given HTTP Provider using the following API:

httpProvider.broadcastWebSocketMessage(WebSocketMessage message);

Viewing WebSocket messages in a browser

By developing an HTTP Provider, you obtain the ability to view information from your own specified URL. For example:

http://[your-wowza-ip-address]:8086/[your-provider]

The following is an example of HTML/Javascript code that corresponds to the WebSocket HTTP Provider, above, and can be used to send and receive message to a web browser. This file must be hosted on a web server as an HTML file. You need to change the wsURL variable to point to your Wowza Streaming Engine instance that's running the WebSocket HTTP Provider:

Thank you for providing feedback to help us improve our documentation!

If you need immediate help for an urgent issue, open a support ticket to get help from one of our technical support engineers. (You must have a valid Maintenance and Support contract to get technical support.)