-
Notifications
You must be signed in to change notification settings - Fork 242
Websockets #2059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+528
−5
Merged
Websockets #2059
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5bbd0da
Add a websocket API to support MCP servers
jrobinso 3f986c6
typo
jrobinso ebc7d4a
Update js/websocket/messageHandler.js
jrobinso 8ef7944
Update js/websocket/messageHandler.js
jrobinso 389176f
Update dev/misc/ws.html
jrobinso a23a148
Update dev/misc/ws.html
jrobinso 5e83275
Update dev/misc/ws.html
jrobinso 1fece4a
add "close" message support
jrobinso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>dev - alignment - bam</title> | ||
| </head> | ||
|
|
||
| <body> | ||
|
|
||
| <h2>Test WebSocket interface</h2> | ||
|
|
||
| <div id="igvDiv" style="padding-top: 50px;padding-bottom: 20px; height: auto"></div> | ||
|
|
||
| <script type="module"> | ||
|
|
||
| import igv from "../../js/index.js" | ||
|
|
||
| const config = { | ||
| genome: "hg19", | ||
| enableWebSocket: true, | ||
| webSocketHost: "localhost", | ||
| webSocketPort: 60141 | ||
| } | ||
|
|
||
| await igv.createBrowser(document.getElementById('igvDiv'), config) | ||
|
|
||
| </script> | ||
|
|
||
| </body> | ||
|
|
||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| const _version = "3.6.0" | ||
| const _version = "3.7.0" | ||
| function version() { | ||
| return _version | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /** | ||
| * Handles incoming messages from the WebSocket connection. Performs requested actions on the IGV browser instance | ||
| * and returns a response message. | ||
| * | ||
| * @param json | ||
| * @param browser | ||
| * @returns {Promise<{uniqueID, message: string, status: string}>} | ||
| */ | ||
|
|
||
|
|
||
| export default async function handleMessage(json, browser) { | ||
|
|
||
| const returnMsg = {uniqueID: json.uniqueID, status: 'ok'} | ||
|
|
||
| try { | ||
| let tracks | ||
| const {type, args} = json | ||
| switch (type.toLowerCase()) { | ||
|
|
||
| case "goto": | ||
| case "search": | ||
| const term = args.locus || args.term | ||
| const found = await browser.search(term) | ||
| if (found) { | ||
| returnMsg.message = `Locus ${term} found and navigated to successfully` | ||
| } else { | ||
| returnMsg.message = `Locus ${term} not found` | ||
| returnMsg.status = 'warning' | ||
| } | ||
| break | ||
|
|
||
| case "currentloci": | ||
| returnMsg.data = browser.currentLoci() | ||
| returnMsg.message = `Retrieved current loci successfully` | ||
| break | ||
|
|
||
| case "visibilityChange": | ||
| returnMsg.message = await browser.visibilityChange() | ||
| break | ||
|
|
||
| case "tojson": | ||
| returnMsg.data = browser.toJSON() | ||
| returnMsg.message = `Session serialized to JSON successfully` | ||
| break | ||
|
|
||
| case "compressedsession": | ||
| returnMsg.data = browser.compressedSession() | ||
| returnMsg.message = `Session serialized and compressed successfully` | ||
| break | ||
|
|
||
| case "tosvg": | ||
| returnMsg.data = browser.toSVG() | ||
| returnMsg.message = `Session exported to SVG successfully` | ||
| break | ||
|
|
||
| case "removetrackbyname": { | ||
| let {trackName} = args | ||
| if(trackName) { | ||
| tracks = browser.findTracks(t => trackName ? t.name === trackName : true) | ||
| if (tracks) { | ||
| tracks.forEach(t => browser.removeTrack(t)) | ||
| returnMsg.message = `Removed track(s) ${trackName} for ${tracks.length} track(s)` | ||
| } else { | ||
| returnMsg.message = `No tracks found matching name ${trackName}` | ||
| returnMsg.status = 'warning' | ||
| } | ||
| } else { | ||
| returnMsg.message = `No track name provided` | ||
| returnMsg.status = 'warning' | ||
| } | ||
| break | ||
| } | ||
|
|
||
| case "loadsampleinfo": { | ||
| browser.loadSampleInfo(args) | ||
| returnMsg.message = `Sample info loaded successfully` | ||
| break | ||
| } | ||
|
|
||
| case "discardsampleinfo": | ||
| browser.discardSampleInfo() | ||
| returnMsg.message = `Sample info discarded successfully` | ||
| break | ||
|
|
||
| case "loadroi": | ||
| browser.loadROI(args) | ||
| returnMsg.message = `ROI loaded successfully` | ||
| break | ||
|
|
||
| case "clearrois": | ||
| browser.clearROIs() | ||
| returnMsg.message = `ROIs cleared successfully` | ||
| break | ||
|
|
||
| case "getuserdefinedrois": | ||
| const rois = await browser.getUserDefinedROIs() | ||
| returnMsg.data = rois | ||
| returnMsg.message = `Retrieved ${rois.length} user-defined ROIs successfully` | ||
| break | ||
|
|
||
| case 'loadtrack': { | ||
| const {url, indexURL} = args | ||
| const track = await browser.loadTrack({url, indexURL}) | ||
| returnMsg.message = `Track ${track.name} loaded successfully` | ||
| break | ||
| } | ||
|
|
||
| case "genome": | ||
| const id = args.id | ||
| await browser.loadGenome(id) | ||
| returnMsg.message = `Genome ${id} loaded successfully` | ||
| break | ||
|
|
||
| case "loadsession": | ||
| const url = args.url | ||
| await browser.loadSession({url}) | ||
| returnMsg.message = `Session loaded successfully from ${url}` | ||
| break | ||
|
|
||
| case "zoomin": | ||
| await browser.zoomIn() | ||
| returnMsg.message = `Zoomed in successfully` | ||
| break | ||
|
|
||
| case "zoomout": | ||
| await browser.zoomOut() | ||
| returnMsg.message = `Zoomed out successfully` | ||
| break | ||
|
|
||
| case "setcolor": | ||
|
|
||
| let {color, trackName} = args | ||
|
|
||
| if (color.includes(",") && !color.startsWith("rgb(")) { | ||
| // Convert "R,G,B" to "rgb(R,G,B)" | ||
| color = `rgb(${color})` | ||
| } | ||
|
|
||
| tracks = browser.findTracks(t => trackName ? t.name === trackName : true) | ||
| if (tracks) { | ||
| tracks.forEach(t => t.color = color) | ||
| browser.repaintViews() | ||
| returnMsg.message = `Set color to ${color} for ${tracks.length} track(s)` | ||
| } else { | ||
| returnMsg.message = `No tracks found matching name ${trackName}` | ||
| returnMsg.status = 'warning' | ||
| } | ||
| break | ||
|
|
||
| case "renametrack": | ||
|
|
||
| const {currentName, newName} = args | ||
|
|
||
| tracks = browser.findTracks(t => currentName === t.name) | ||
| if (tracks && tracks.length > 0) { | ||
| tracks.forEach(t => { | ||
| t.name = newName | ||
| browser.fireEvent('tracknamechange', [t]) | ||
| }) | ||
| returnMsg.message = `Renamed ${tracks.length} track(s) from ${currentName} to ${newName}` | ||
| } else { | ||
| returnMsg.message = `No track found with name ${currentName}` | ||
| returnMsg.status = 'warning' | ||
| } | ||
| break | ||
|
|
||
| default: | ||
| returnMsg.message = `Unrecognized message type: ${type}` | ||
| returnMsg.status = 'error' | ||
| } | ||
| } catch (err) { | ||
| returnMsg.message = err?.message || String(err) | ||
| returnMsg.status = 'error' | ||
| } | ||
|
|
||
| return returnMsg | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import handleMessage from "./messageHandler.js" | ||
|
|
||
| /** | ||
| * Create a WebSocket client that connects to a server and handles messages. The client attempts to connect to a | ||
| * WebSocketServer upon creation. If the connection is not successful or lost, it will attempt to reconnect with an | ||
| * exponential backoff strategy. Incoming messages are expected to be JSON formatted and are processed by the | ||
| * handleMessage function. Messages encompass a subset of the igv.js API | ||
| * | ||
| * This client was created to interact with an MCP server, but could be used for other purposes. | ||
| * | ||
| * @param host Host for the WebSocket server | ||
| * @param port Port for the WebSocket server | ||
| * @param browser The igv.js browser instance | ||
| */ | ||
|
|
||
| export function createWebSocketClient(host, port, browser) { | ||
|
|
||
| let socket | ||
| let retryInterval = 1000 // Initial retry interval in ms | ||
| const maxRetryInterval = 10000 // Maximum retry interval in ms | ||
| let reconnectTimer | ||
| let intentionalClose = false // Flag to prevent reconnection on intentional close | ||
|
|
||
| function connect() { | ||
|
|
||
| const isLocal = host === 'localhost' || host === '127.0.0.1' | ||
| const protocol = window.location.protocol === 'https:' && !isLocal ? 'wss:' : 'ws:' | ||
| socket = new WebSocket(`${protocol}//${host}:${port}`) | ||
|
|
||
| // helper to safely send | ||
| const sendJSON = (obj) => { | ||
| if (socket.readyState === WebSocket.OPEN) { | ||
| socket.send(JSON.stringify(obj)) | ||
| } | ||
| } | ||
|
|
||
| socket.addEventListener('open', function (event) { | ||
| retryInterval = 1000 // Reset retry interval on successful connection | ||
| sendJSON({message: 'Hello from browser client'}) | ||
| }) | ||
|
|
||
| // Listen for incoming messages | ||
| socket.addEventListener('message', async function (event) { | ||
| try { | ||
| const json = JSON.parse(event.data) | ||
|
|
||
| if("close" === json.type) { | ||
| intentionalClose = true | ||
| clearTimeout(reconnectTimer) | ||
| if (socket && socket.readyState === WebSocket.OPEN) { | ||
| socket.close() | ||
| } | ||
| return | ||
| } | ||
|
|
||
| const returnMsg = await handleMessage(json, browser) | ||
| sendJSON(returnMsg) | ||
|
|
||
| } catch (e) { | ||
| if (e instanceof SyntaxError) { | ||
| console.warn('Received non-JSON message from server:', event.data) | ||
| } else { | ||
| console.error('Error handling message:', e) | ||
| sendJSON({ | ||
| status: 'error', | ||
| message: `Error handling message: ${e.message || e.toString()}` | ||
| }) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| socket.addEventListener('error', function (event) { | ||
| console.error('WebSocket error:', event) | ||
| // The 'close' event will fire immediately after 'error', triggering the reconnect logic. | ||
| }) | ||
|
|
||
| socket.addEventListener('close', function (event) { | ||
| if (intentionalClose) { | ||
| console.log('WebSocket closed intentionally. Not reconnecting.') | ||
| return | ||
| } | ||
| console.log('Disconnected from server. Retrying in ' + (retryInterval / 1000) + ' seconds.') | ||
| clearTimeout(reconnectTimer) | ||
| reconnectTimer = setTimeout(connect, retryInterval) | ||
| // Increase retry interval for next time, up to a max | ||
| retryInterval = Math.min(maxRetryInterval, retryInterval * 2) | ||
| }) | ||
| } | ||
|
|
||
| connect() // Initial connection attempt | ||
|
|
||
| window.addEventListener('beforeunload', function (event) { | ||
| clearTimeout(reconnectTimer) // Don't try to reconnect when page is closing | ||
| if (socket && socket.readyState === WebSocket.OPEN) { | ||
| socket.close() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| export default createWebSocketClient |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.