PrCore employs SSE to stream prescriptions to the client in a timely manner. For SSE, you can check here for more information.
Note
The status of the project should be STREAMING or SIMULATING, otherwise the SSE endpoint will return 400 error.
Warning
If the user attempts to read results from a project that is already being read by another client, the SSE endpoint will return a 400 error. As it is specifically designed to read results from only one client at a time, it is not advisable to attempt to read results from the same project using multiple clients.
Request
Please note that the SSE endpoint is not a REST endpoint. It is a persistent connection endpoint. You should use a client that supports SSE to connect to the endpoint.
Method
Endpoint
Request body type
Description
GET
/project/{project_id}/stream/result
none
Get event stream of the prescriptions
Response
A message will be sent to the client every time a new prescription is available. The message will be in the following format:
event: message
id: 1234
data: []
The data field is a list containing prescriptions. The format of the prescription is described in the following section.
Also, when connection is established, the client will receive a message with the following format:
event: notification
id: 0
data: CONNECTED
After the simulation is stopped, the client will receive a message with the following format:
event: notification
id: 54321
data: FINISHED
The program will also send ping messages to the client every 15 seconds:
If the user’s Postman version is v10 or higher, they may utilize the following collection to test the SSE endpoint. Since Postman has altered the method for testing SSE, additional information on this topic can be found here.
Below is a GIF that shows the prescriptions SSE endpoint being tested with Postman:
Example client script
Here is a sample script that connects to the PrCore SSE endpoint and outputs the prescriptions. This script can be customized to meet the user’s specific requirements. It is important to note that the PROJECT_ID value should be replaced with the actual project ID.
importjsonimportpprintimportrequestsimportsseclientBASE_URL="http**********"# Please change this to your local instance addressPROJECT_ID=1URL=f"{BASE_URL}/project/{PROJECT_ID}/stream/result"HEADERS={"Authorization":"Bearer UaJW0QvkMA1cVnOXB89E0NbLf3JRRoHwv2wWmaY5v=QYpaxr1UD9/FupeZ85sa2r"}defwith_requests(url,headers):# Get a streaming response for the given event feed using requests.returnrequests.get(url,stream=True,headers=headers)response=with_requests(URL,HEADERS)client=sseclient.SSEClient(response)print("Waiting for events...")"""
Please note that the word `event` here is the `E` from `SSE`,
not the `event` in the business processing perspective.
The data of our `event` is in the `event.data` list.
"""try:foreventinclient.events():ifevent.event!="message":continueevent_data=json.loads(event.data)first_event=event_data[0]prescriptions=first_event["prescriptions"]prescriptions_with_output=[prescriptions[p]forpinprescriptionsifprescriptions[p]["output"]]ifnotprescriptions_with_output:continueprint(f"Received message: {event.event}")print(f"ID: {event.id}")print(f"Data type: {type(event_data)}")print(f"Length: {len(event_data)}")pprint.pprint(prescriptions_with_output,width=120)print("-"*24)exceptKeyboardInterrupt:print("Interrupted by user")print("Done!")
To run this script, please install the sseclient package first.
The following is an example of the event.data object that is received from the SSE endpoint.
The case_completed attribute is a boolean value that indicates whether or not the case has been completed. If the case is complete, the prescriptions array will be empty.
It is worth noting that event.data is a list, which implies that it may contain multiple events, particularly when the user is connecting to the SSE endpoint for the first time, as there may be some events already in the queue.