onvif
    Preparing search index...

    onvif

    ONVIF

    Coverage Status

    ONVIF client protocol implementation for Node.js.

    Tip

    This page describes the future 1.x version of the ONVIF library written in TypeScript. If you are looking for the README for the stable 0.x version, please see branch v0.x

    The default npm installation still uses version 0.x. If you want to try this new version, install it with:

    npm install onvif@alpha
    

    This is a wrapper for the ONVIF protocol that allows you to:

    • get information about your NVT (Network Video Transmitter) device and its media sources
    • control PTZ (pan-tilt-zoom) movements
    • manage presets
    • detect devices on your network
    • control events
    • retrieve information about your NVR (Network Video Recorder) Profile G device
    • obtain a list of recordings

    The library runs on Node.js and works server-side.

    ONVIF

    This is a new version of the ONVIF library. The previous version was written in JavaScript, while this version is written in TypeScript and includes interfaces describing ONVIF data structures.

    At the moment, some of the methods from v0.8 have been implemented.

    You can find the list of supported ONVIF commands here:

    The library will continue to be updated, as other methods are currently under development.

    The documentation for the new library was generated with TypeDoc and is available here:

    Code that uses the old version of the library (0.8.x) should work with the compatibility class:

    Warning

    Compatibility mode is currently unsupported.

    Thanks a lot for your interest!
    I will be happy to answer any questions and hear your feedback.



    Connection

    Before you can use most library methods, call connect() on your Onvif instance. This method performs the initial handshake with the device and fills internal state so later SOAP requests are authenticated and routed to the correct service endpoints.

    connect() runs the following steps in order:

    1. Time synchronizationgetSystemDateAndTime() is called first. ONVIF WS-Security authentication includes a timestamp in the nonce digest, so the client must know the offset between its own clock and the device clock (timeShift). The library tries an unauthenticated request first, as the ONVIF spec allows, and retries with credentials when the device requires authentication (some Panasonic and Digital Barriers models behave this way).
    2. Service discovery — the library tries device.getServices(), the modern ONVIF approach introduced with Profile T. If that fails on older devices, it falls back to device.getCapabilities(). Both methods populate onvif.uri with the URLs for media, PTZ, events, replay, and other services that subsequent requests use.
    3. Media configurationmedia.getProfiles() and media.getVideoSources() run in parallel, then getActiveSources() matches each video source to a suitable media profile. This sets activeSource, defaultProfile, and defaultProfiles, including encoder settings and PTZ configuration, so you can start streaming or controlling the camera without extra setup.

    On success, connect() emits a connect event and returns the Onvif instance. Pass autoConnect: true in the constructor to run this automatically after instantiation.

    const onvif = new Onvif({ hostname: '192.168.1.13', port: 8000, username: 'admin', password: 'admin' });
    await onvif.connect();
    console.log(onvif.activeSource);

    Events

    To subscribe to all events using pull-point subscription you can just use .on() method, since the Onvif class inherits from the EventEmitter class.

    const onvif = new Onvif();
    function eventHandler(msg) {
    console.log(msg);
    onvif.off('event');
    }
    onvif.on('event', eventHandler);

    If you need to subscribe to events, you can use the Subscription class. This class is for the specific subscriptions, for example, when we need to subscribe to events from the camera with the filters. or add some more subscriptions than the common one. It uses the pull-point subscription. It inherits from EventEmitter. And emits two events: data and error. To use it you need to call subscribe() method. And to stop the device subscription and remove all listeners you need to call unsubscribe() method.

    The first and the only one argument for data is the NotificationMessage object. And an error raised only when the connection to the device is lost.

    await cam.connect();
    const sub = new Subscription(cam, {
    filter: {
    topicExpression: [
    {
    expression: 'tns1:RuleEngine/CellMotionDetector/Motion',
    dialect: 'http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet',
    },
    ],
    },
    });
    sub.on('data', async (data) => {
    console.log(new Date().toLocaleTimeString(), 'motion', data.topic._, data.message.message.data);
    await sub.unsubscribe();
    });
    await sub.subscribe();

    For a full interactive example, see events.with.filter.ts.

    This class is used internally by the Onvif class for the common event listener.

    With push (WS-BaseNotification), the camera sends event notifications to an HTTP endpoint you host, instead of you polling the device.

    To use it: start an HTTP server reachable from the camera, call subscribe with that URL as the consumer reference, keep the subscription alive with renew before it expires, and call unsubscribe when you are done. Method signatures are in the Events class documentation. A working flow is shown in events.with.filter.ts — the HTTP server at lines 50–65, and subscribe / unsubscribe at lines 143–164.


    Interfaces

    Interfaces are generated according to the latest version of the ONVIF specification.

    All methods accept options defined by the ONVIF specification and return data from the corresponding <method_name>Response.

    For example, the getCapabilities method accepts a single argument of type GetCapabilities and returns a result of type Capabilities.

    Below is the internal structure of the GetCapabilitiesResponse type:

    export interface GetCapabilitiesResponse {
    /** Capability information. */
    capabilities?: Capabilities;
    }

    class Device {
    // ...

    async getCapabilities(options?: GetCapabilities): Promise<Capabilities> {
    // ...
    }

    // ...
    }

    In general, the library tries to avoid returning objects that contain only a single property.

    In some cases, where native JavaScript types are more convenient, interfaces are extended with additional fields.

    For example:

    • SetSystemDateAndTime
    • SetSystemDateAndTimeExtended

    The extended version adds a more convenient field:

    export interface SetSystemDateAndTimeExtended extends SetSystemDateAndTime {
    /**
    * Javascript Date object to use instead of UTCDateTime
    */
    dateTime?: Date;
    // ...
    }

    xs:any Support

    The ONVIF specifications include numerous extension points, which presents a challenge:

    • on one hand, we want simple and convenient interfaces
    • on the other hand, we need a unified mechanism for handling undocumented vendor-specific data

    This data is usually provided through:

    <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
    

    This mechanism is important for:

    • backward compatibility
    • forward compatibility
    • XML ↔ JavaScript transformation in ONVIF get/set methods

    Suppose we have an ElementItem structure defined in:

    Example schema:

    <xs:element name="ElementItem" minOccurs="0" maxOccurs="unbounded">
    <xs:annotation>
    <xs:documentation>Complex value structure.</xs:documentation>
    </xs:annotation>

    <xs:complexType>
    <xs:sequence>
    <xs:any namespace="##any" processContents="lax">
    <xs:annotation>
    <xs:documentation>
    XML tree containing the element value as defined
    in the corresponding description.
    </xs:documentation>
    </xs:annotation>
    </xs:any>
    </xs:sequence>

    <xs:attribute name="Name" type="xs:string" use="required">
    <xs:annotation>
    <xs:documentation>Item name.</xs:documentation>
    </xs:annotation>
    </xs:attribute>
    </xs:complexType>
    </xs:element>

    This becomes the following autogenerated TypeScript interface:

    export interface ElementItem {
    /** Item name. */
    name: string;

    /** XML tree containing the element value as defined in the corresponding description. */
    [key: string]: unknown;
    }

    For example, in MetadataConfiguration:

    <Parameters>
    <ElementItem>
    <Name>elementItem1</Name>

    <Param1>
    <Data>42</Data>
    </Param1>

    <Param2>param2</Param2>
    </ElementItem>
    </Parameters>

    After parsing, the object looks like this:

    {
    elementItem : [{
    name : 'elementItem1',

    param1 : {
    data : 42
    },

    param2 : 'param2',

    __any__ : {
    'Name' : ['elementItem1'],

    'Param1' : [{
    'Data' : ['42']
    }],

    'Param2' : 'param2'
    }
    }]
    }

    This object contains:

    • the required name field
    • parsed xs:any fields (param1, param2)
    • the raw __any__ field

    The __any__ field contains the original unprocessed object returned by xml2js


    A reasonable question is:

    Why keep the raw XML structure?

    The answer is simple.

    When configuring ONVIF devices, extensions, or vendor-specific parameters, we often do not know how to serialize a clean JavaScript object back into the correct XML structure.

    At the same time, we still want to work with the data in a convenient way.

    So when modifying device configuration (for example using setMetadataConfiguration), follow two simple rules:

    elementItem[0].name = 'hello'
    
    elementItem[0].__any__.Param2 = 'hi'
    

    This structure can then be automatically converted back into the appropriate SOAP XML.


    Tests

    All tests are written using Jest.

    Run them with:

    npm test
    

    The tests use happytime-onvif-server as a test device.

    Thanks to HappyTimeSoft for providing the opportunity to test the full ONVIF specification.

    Products are available here: