### Install LGTV2 Module Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Install the LGTV2 module using npm. ```bash npm install lgtv2 ``` -------------------------------- ### Launch an Application Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Starts a specific application on the TV using its ID. ```javascript lgtv.request('ssap://system.launcher/launch', {id: 'netflix'}); ``` -------------------------------- ### List, Launch, and Manage LG TV Apps Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Use this snippet to list installed applications, get the currently foreground app, launch specific apps by ID, open URLs in the browser, and close applications. Requires the lgtv2 library and a valid TV connection URL. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { // List all available launch points (installed apps) lgtv.request('ssap://com.webos.applicationManager/listLaunchPoints', function(err, res) { if (!err && res.launchPoints) { res.launchPoints.forEach(function(app) { console.log('App:', app.title, '- ID:', app.id); }); } }); // Get foreground app info lgtv.request('ssap://com.webos.applicationManager/getForegroundAppInfo', function(err, res) { console.log('Current app:', res.appId); }); // Launch an app by ID lgtv.request('ssap://system.launcher/launch', {id: 'youtube.leanback.v4'}); // Launch Netflix lgtv.request('ssap://system.launcher/launch', {id: 'netflix'}); // Launch Amazon Prime Video lgtv.request('ssap://system.launcher/launch', {id: 'amazon'}); // Open a URL in the browser lgtv.request('ssap://system.launcher/open', {target: 'https://www.google.com'}); // Close an app lgtv.request('ssap://system.launcher/close', {id: 'netflix'}); }); lgtv.on('error', function(err) { console.error(err); }); ``` -------------------------------- ### Get Pointer Input Socket Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Obtain a specialized socket connection for mouse and button events on the TV. This example demonstrates how to send a 'click' event. ```javascript lgtv.getSocket( 'ssap://com.webos.service.networkinput/getPointerInputSocket', function(err, sock) { if (!err) { sock.send('click'); } } ); ``` -------------------------------- ### LGTV2 Module Usage Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Examples demonstrating how to use the LGTV2 module to connect to an LG WebOS TV, subscribe to events, and send commands. ```APIDOC ## Subscribe to volume and mute changes ```javascript var lgtv = require("lgtv2")({ url: 'ws://lgwebostv:3000' }); lgtv.on('error', function (err) { console.log(err); }); lgtv.on('connect', function () { console.log('connected'); lgtv.subscribe('ssap://audio/getVolume', function (err, res) { if (res.changed.indexOf('volume') !== -1) console.log('volume changed', res.volume); if (res.changed.indexOf('muted') !== -1) console.log('mute changed', res.muted); }); }); ``` ## Turn TV off ```javascript var lgtv = require("lgtv2")({ url: 'ws://lgwebostv:3000' }); lgtv.on('error', function (err) { console.log(err); }); lgtv.on('connect', function () { console.log('connected'); lgtv.request('ssap://system/turnOff', function (err, res) { lgtv.disconnect(); }); }); ``` ``` -------------------------------- ### getSocket(uri, callback) Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Get a specialized socket connection for mouse pointer input and button events. ```APIDOC ## getSocket(uri, callback) ### Description Get a specialized socket connection for mouse pointer input and button events. This provides low-level control for simulating remote control button presses and pointer movement. ### Parameters - **uri** (string) - Required - The SSAP URI for the input socket. - **callback** (function) - Required - Function to handle the socket connection, receiving (err, sock). ``` -------------------------------- ### Control LG TV Channels Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Manage TV channels by fetching channel lists, getting current channel information, switching channels, and managing external inputs like HDMI. Requires the lgtv2 library and a valid TV connection URL. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { // Get list of all channels lgtv.request('ssap://tv/getChannelList', function(err, res) { if (!err && res.channelList) { res.channelList.forEach(function(channel) { console.log('Channel:', channel.channelNumber, '-', channel.channelName); }); } }); // Get current channel lgtv.request('ssap://tv/getCurrentChannel', function(err, res) { console.log('Current channel:', res.channelName); }); // Get program info for current channel lgtv.request('ssap://tv/getChannelProgramInfo', function(err, res) { console.log('Current program:', res); }); // Channel up lgtv.request('ssap://tv/channelUp'); // Channel down lgtv.request('ssap://tv/channelDown'); // Switch to specific channel by ID lgtv.request('ssap://tv/openChannel', {channelId: '1_23_4_5_6_7'}); // Get external input list (HDMI, AV, etc.) lgtv.request('ssap://tv/getExternalInputList', function(err, res) { if (!err && res.devices) { res.devices.forEach(function(input) { console.log('Input:', input.label, '- ID:', input.id); }); } }); // Switch to HDMI input lgtv.request('ssap://tv/switchInput', {inputId: 'HDMI_1'}); }); lgtv.on('error', function(err) { console.error(err); }); ``` -------------------------------- ### Execute System Commands with lgtv2 Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Demonstrates how to initialize a connection and execute various system commands such as turning off the TV, showing notifications, and querying service information. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { // Turn off the TV lgtv.request('ssap://system/turnOff', function(err, res) { if (!err) { console.log('TV turned off'); lgtv.disconnect(); } }); // Show toast notification lgtv.request('ssap://system.notifications/createToast', { message: 'Hello from Node.js!' }); // Get current software information lgtv.request('ssap://com.webos.service.update/getCurrentSWInformation', function(err, res) { if (!err) { console.log('Product name:', res.product_name); console.log('Firmware version:', res.major_ver + '.' + res.minor_ver); } }); // Get list of available services lgtv.request('ssap://api/getServiceList', function(err, res) { if (!err && res.services) { res.services.forEach(function(service) { console.log('Service:', service.name); }); } }); // Enable 3D mode lgtv.request('ssap://com.webos.service.tv.display/set3DOn'); // Disable 3D mode lgtv.request('ssap://com.webos.service.tv.display/set3DOff'); // Send text input (for on-screen keyboards) lgtv.request('ssap://com.webos.service.ime/sendEnterKey'); // Delete characters in text input lgtv.request('ssap://com.webos.service.ime/deleteCharacters', {count: 3}); }); lgtv.on('error', function(err) { console.error(err); }); ``` -------------------------------- ### Initialize LGTV2 Instance Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Configure the TV connection with WebSocket settings, timeouts, and custom key storage mechanisms. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000', // WebSocket URL of your TV timeout: 15000, // Request timeout in ms (default: 15000) reconnect: 5000, // Reconnect interval in ms (default: 5000, false to disable) keyFile: '/path/to/keyfile', // Custom path for key storage wsconfig: { // WebSocket client configuration keepalive: true, keepaliveInterval: 10000, dropConnectionOnKeepaliveTimeout: true, keepaliveGracePeriod: 5000 } }); // Custom key storage implementation var lgtvCustom = require('lgtv2')({ url: 'ws://192.168.1.100:3000', clientKey: 'your-saved-client-key', // Provide previously saved key saveKey: function(key, callback) { // Custom key save function myDatabase.save('lgtv_key', key, callback); } }); ``` -------------------------------- ### Manage input sockets with getSocket() Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Provides a low-level socket for simulating remote control button presses, pointer movement, and scrolling. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { // Get pointer input socket lgtv.getSocket('ssap://com.webos.service.networkinput/getPointerInputSocket', function(err, sock) { if (err) { console.error('Error getting socket:', err); return; } // Simulate a click sock.send('click'); // Simulate button presses sock.send('button', {name: 'HOME'}); sock.send('button', {name: 'ENTER'}); sock.send('button', {name: 'BACK'}); sock.send('button', {name: 'UP'}); sock.send('button', {name: 'DOWN'}); sock.send('button', {name: 'LEFT'}); sock.send('button', {name: 'RIGHT'}); // Move pointer (relative movement) sock.send('move', {dx: 100, dy: 50}); // Scroll sock.send('scroll', {dx: 0, dy: -100}); // Close the specialized socket when done // sock.close(); }); }); lgtv.on('error', function(err) { console.error(err); }); ``` -------------------------------- ### request(uri, payload, callback) Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Sends a one-time command to the LG WebOS TV using the SSAP protocol. ```APIDOC ## request(uri, payload, callback) ### Description Sends a one-time request to the TV to perform an action, such as changing volume, launching an app, or turning off the device. ### Parameters - **uri** (string) - Required - The SSAP URI endpoint (e.g., 'ssap://system/turnOff'). - **payload** (object) - Optional - The command parameters (e.g., {volume: 15}). - **callback** (function) - Required - Function to handle the response or error (err, res). ### Request Example // Set volume to 15 lgtv.request('ssap://audio/setVolume', {volume: 15}, function(err, res) { if (err) console.error('Error:', err); else console.log('Volume set to 15'); }); ### Response - **err** (object) - Error object if the request failed. - **res** (object) - Response payload from the TV. ``` -------------------------------- ### Handle Connection Events Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Listen for lifecycle events such as connection attempts, authorization prompts, and errors to manage the TV session. ```javascript var lgtv = require('lgtv2')({ url: 'ws://lgwebostv:3000' }); // Fired when attempting to connect lgtv.on('connecting', function(host) { console.log('Connecting to TV at:', host); }); // Fired when TV prompts for authorization (first connection) lgtv.on('prompt', function() { console.log('Please accept the connection on your TV screen'); }); // Fired when successfully connected and authorized lgtv.on('connect', function() { console.log('Connected and ready to send commands'); }); // Fired on connection errors lgtv.on('error', function(err) { console.error('Connection error:', err); }); // Fired when connection closes lgtv.on('close', function() { console.log('Connection closed'); }); // Fired for all raw messages (useful for debugging) lgtv.on('message', function(message) { console.log('Raw message:', message); }); ``` -------------------------------- ### Control TV audio with request() Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Use these commands to adjust volume, mute, or retrieve current audio status. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { // Get current audio status lgtv.request('ssap://audio/getStatus', function(err, res) { console.log('Audio status:', res); }); // Get current volume lgtv.request('ssap://audio/getVolume', function(err, res) { console.log('Current volume:', res.volume, 'Muted:', res.muted); }); // Set specific volume level (0-100) lgtv.request('ssap://audio/setVolume', {volume: 25}); // Volume up lgtv.request('ssap://audio/volumeUp'); // Volume down lgtv.request('ssap://audio/volumeDown'); // Mute lgtv.request('ssap://audio/setMute', {mute: true}); // Unmute lgtv.request('ssap://audio/setMute', {mute: false}); }); lgtv.on('error', function(err) { console.error(err); }); ``` -------------------------------- ### Execute TV Commands via Request Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Send one-time SSAP commands to the TV, such as power control, volume adjustment, or launching applications. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { // Turn off the TV lgtv.request('ssap://system/turnOff', function(err, res) { if (err) console.error('Error:', err); else console.log('TV turned off'); lgtv.disconnect(); }); // Set volume to 15 lgtv.request('ssap://audio/setVolume', {volume: 15}, function(err, res) { if (err) console.error('Error:', err); else console.log('Volume set to 15'); }); // Mute the TV lgtv.request('ssap://audio/setMute', {mute: true}, function(err, res) { if (err) console.error('Error:', err); else console.log('TV muted'); }); // Launch Netflix lgtv.request('ssap://system.launcher/launch', {id: 'netflix'}, function(err, res) { if (err) console.error('Error:', err); else console.log('Netflix launched'); }); // Show a toast notification lgtv.request('ssap://system.notifications/createToast', {message: 'Hello from Node.js!'}, function(err, res) { if (err) console.error('Error:', err); else console.log('Toast notification sent'); }); }); lgtv.on('error', function(err) { console.error(err); }); ``` -------------------------------- ### LGTV2 Commands Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md A list of available commands that can be sent to the LG WebOS TV using the LGTV2 module. ```APIDOC ## Commands ### `api/getServiceList` ### `audio/setMute` - **Description**: Enable/Disable mute. - **Example**: `lgtv.request('ssap://audio/setMute', {mute: true});` ### `audio/getStatus` ### `audio/getVolume` ### `audio/setVolume` - **Description**: Set the TV volume. - **Example**: `lgtv.request('ssap://audio/setVolume', {volume: 10});` ### `audio/volumeUp` ### `audio/volumeDown` ### `com.webos.applicationManager/getForegroundAppInfo` ### `com.webos.applicationManager/launch` ### `com.webos.applicationManager/listLaunchPoints` ### `com.webos.service.appstatus/getAppStatus` ### `com.webos.service.ime/sendEnterKey` ### `com.webos.service.ime/deleteCharacters` ### `com.webos.service.tv.display/set3DOn` ### `com.webos.service.tv.display/set3DOff` ### `com.webos.service.update/getCurrentSWInformation` ### `media.controls/play` - **Example**: `lgtv.request('ssap://media.controls/play');` ### `media.controls/stop` ### `media.controls/pause` - **Example**: `lgtv.request('ssap://media.controls/pause');` ### `media.controls/rewind` ### `media.controls/fastForward` ``` -------------------------------- ### LGTV2 API Methods Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Details on the core methods provided by the LGTV2 module for interacting with the TV. ```APIDOC ## `request(url [, payload] [, callback])` ### Description Sends a request to the TV. ### Parameters - **url** (string) - Required - The endpoint URL for the request. - **payload** (object) - Optional - The data to send with the request. - **callback** (function) - Optional - A function to handle the response. ## `subscribe(url, callback)` ### Description Subscribes to a specific event stream from the TV. ### Parameters - **url** (string) - Required - The endpoint URL to subscribe to. - **callback** (function) - Required - A function to handle incoming data. ## `disconnect()` ### Description Closes the connection to the TV and stops auto-reconnection. ## `getSocket(url, callback)` ### Description Gets a specialized socket connection for mouse and button events. ### Parameters - **url** (string) - Required - The endpoint URL for the socket. - **callback** (function) - Required - A function to handle the socket connection and events. ### Request Example ```javascript lgtv.getSocket( 'ssap://com.webos.service.networkinput/getPointerInputSocket', function(err, sock) { if (!err) { sock.send('click'); } } ); ``` ``` -------------------------------- ### Subscribe to TV state changes with subscribe() Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Use this to receive continuous updates for specific TV states like volume, foreground app, or channel information. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { // Subscribe to volume changes lgtv.subscribe('ssap://audio/getVolume', function(err, res) { if (err) { console.error('Subscription error:', err); return; } if (res.changed && res.changed.indexOf('volume') !== -1) { console.log('Volume changed to:', res.volume); } if (res.changed && res.changed.indexOf('muted') !== -1) { console.log('Mute status changed to:', res.muted); } }); // Subscribe to foreground app changes lgtv.subscribe('ssap://com.webos.applicationManager/getForegroundAppInfo', function(err, res) { if (err) { console.error('Subscription error:', err); return; } console.log('Current app:', res.appId); }); // Subscribe to current channel lgtv.subscribe('ssap://tv/getCurrentChannel', function(err, res) { if (err) { console.error('Subscription error:', err); return; } console.log('Current channel:', res.channelName, '- Number:', res.channelNumber); }); }); lgtv.on('error', function(err) { console.error(err); }); ``` -------------------------------- ### subscribe(uri, callback) Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Subscribe to TV state changes and receive continuous updates when values change. ```APIDOC ## subscribe(uri, callback) ### Description Subscribe to TV state changes and receive continuous updates when values change. Useful for monitoring volume, current app, channel, and other dynamic states. ### Parameters - **uri** (string) - Required - The SSAP URI to subscribe to. - **callback** (function) - Required - Function to handle updates, receiving (err, res). ``` -------------------------------- ### Media Play Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Send a request to the TV to play media. ```javascript lgtv.request('ssap://media.controls/play'); ``` -------------------------------- ### Set Volume Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Send a request to the TV to set the volume to a specific level. ```javascript lgtv.request('ssap://audio/setVolume', {volume: 10}); ``` -------------------------------- ### Create a Toast Notification Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Displays a popup message on the TV screen. ```javascript lgtv.request('ssap://system.notifications/createToast', {message: 'Hello World!'}); ``` -------------------------------- ### Audio Control Commands Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Control TV audio including volume adjustment and mute functionality. ```APIDOC ## Audio Control Commands ### Description Control TV audio including volume adjustment and mute functionality using the request method. ### Supported Endpoints - **ssap://audio/getStatus** - Get current audio status. - **ssap://audio/getVolume** - Get current volume and mute status. - **ssap://audio/setVolume** - Set volume level (0-100). - **ssap://audio/volumeUp** - Increase volume. - **ssap://audio/volumeDown** - Decrease volume. - **ssap://audio/setMute** - Set mute status (true/false). ``` -------------------------------- ### Subscribe to Volume and Mute Changes Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Connect to the TV and subscribe to volume and mute status updates. Requires the TV to be accessible at 'ws://lgwebostv:3000'. ```javascript var lgtv = require("lgtv2")({ url: 'ws://lgwebostv:3000' }); lgtv.on('error', function (err) { console.log(err); }); lgtv.on('connect', function () { console.log('connected'); lgtv.subscribe('ssap://audio/getVolume', function (err, res) { if (res.changed.indexOf('volume') !== -1) console.log('volume changed', res.volume); if (res.changed.indexOf('muted') !== -1) console.log('mute changed', res.muted); }); }); ``` -------------------------------- ### disconnect() Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Close the connection to the TV and stop auto-reconnection. ```APIDOC ## disconnect() ### Description Close the connection to the TV and stop auto-reconnection. Use this when you're done communicating with the TV. ``` -------------------------------- ### Media Pause Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Send a request to the TV to pause media playback. ```javascript lgtv.request('ssap://media.controls/pause'); ``` -------------------------------- ### LGTV2 Events Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md List of events emitted by the LGTV2 module, indicating different states and occurrences during the connection and operation. ```APIDOC ## Events ### `prompt` - **Description**: Emitted when the TV prompts for App authorization. ### `connect` - **Description**: Emitted when a connection is established and authorized. ### `connecting` - **Description**: Emitted when trying to connect to the TV. ### `close` - **Description**: Emitted when the connection to the TV is closed. ### `error` - **Description**: Emitted when Websocket connection errors occur. Subsequent equal errors will only be emitted once to prevent log flooding. ``` -------------------------------- ### Disconnect from TV with disconnect() Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Closes the connection and stops auto-reconnection attempts. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { console.log('Connected'); // Perform some action then disconnect lgtv.request('ssap://audio/getVolume', function(err, res) { if (!err) { console.log('Current volume:', res.volume); } // Disconnect from TV (also stops auto-reconnect) lgtv.disconnect(); console.log('Disconnected from TV'); }); }); lgtv.on('close', function() { console.log('Connection closed'); process.exit(0); }); lgtv.on('error', function(err) { console.error(err); }); ``` -------------------------------- ### Set Mute Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Send a request to the TV to enable or disable mute. ```javascript lgtv.request('ssap://audio/setMute', {mute: true}); ``` -------------------------------- ### Turn Off TV Source: https://github.com/hobbyquaker/lgtv2/blob/master/README.md Connect to the TV and send a command to turn it off, then disconnect. Requires the TV to be accessible at 'ws://lgwebostv:3000'. ```javascript var lgtv = require("lgtv2")({ url: 'ws://lgwebostv:3000' }); lgtv.on('error', function (err) { console.log(err); }); lgtv.on('connect', function () { console.log('connected'); lgtv.request('ssap://system/turnOff', function (err, res) { lgtv.disconnect(); }); }); ``` -------------------------------- ### Control LG TV Media Playback Source: https://context7.com/hobbyquaker/lgtv2/llms.txt Control media playback functions such as play, pause, stop, fast forward, and rewind for compatible apps. Also includes functionality to close the media viewer. Requires the lgtv2 library and a valid TV connection URL. ```javascript var lgtv = require('lgtv2')({ url: 'ws://192.168.1.100:3000' }); lgtv.on('connect', function() { // Play lgtv.request('ssap://media.controls/play'); // Pause lgtv.request('ssap://media.controls/pause'); // Stop lgtv.request('ssap://media.controls/stop'); // Fast forward lgtv.request('ssap://media.controls/fastForward'); // Rewind lgtv.request('ssap://media.controls/rewind'); // Close media viewer lgtv.request('ssap://media.viewer/close'); }); lgtv.on('error', function(err) { console.error(err); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.