Skip to content
Joshua Shinavier edited this page Oct 9, 2023 · 3 revisions

SmSn follows a client-server pattern in which there are multiple potential clients, and a single server application called SmSn Server. This page assumes that you have already started SmSn server, and contains details on communicating with it.

SmSn Server normally piggybacks on Gremlin Server, and re-uses the latter's default WebSocket port of 8182; connect to ws://localhost:8182/gremlin.

The following is a simple program in Python (suitable for a Jupyter Notebook) which you can use to experiment with SmSn's WebSocket communication:

import asyncio
import websockets
import json

def toGremlinRequestJson(action_json):
  return {
    "op":"eval",
    "processor":"session",
    "args":{
        "language":"smsn",
        "session":"undefined",
        "gremlin":json.dumps(action_json)}}
    
async def issueSmSnRequest(actionJson, callback):
    gremlinServerUrl = "ws://localhost:8182/gremlin"

    try:
        async with websockets.connect(gremlinServerUrl) as websocket:
            jsonString = json.dumps(toGremlinRequestJson(actionJson))

            # Send the JSON string to the WebSocket server
            await websocket.send(jsonString)

            # Receive and print the response from the WebSocket server
            response = await websocket.recv()
            responseJson = json.loads(response)
            if responseJson["status"]["code"] == 200:
                callback(json.loads(responseJson["result"]["data"][0]))
            else:
                print("Error status:")
                print(responseJson["status"])
            
            callback(response)

    except Exception as e:
        print(f"An error occurred: {e}")

def printResult(result):
    print(f"Result from SmSn server: {result}")

async def smsnSearch(searchTerm, callback=printResult):
    actionJson = {
        "action":"net.fortytwo.smsn.server.actions.Search",
        "filter":{
            "minSource":"private",
            "defaultSource":"private",
            "minWeight":0.0,
            "defaultWeight":0.5},
        "titleCutoff":100,
        "style":"forward",
        "queryType":"FullText",
        "query":searchTerm,
        "height":1}
    await issueSmSnRequest(actionJson, lambda r: callback(r["view"]["children"]))

Now apply the above to a search term, e.g.

await smsnSearch("*hello*")