# AMPER Channels

# How to use amper-channels microservice (websockets)

Short instruction how to implement communication with microservice.


## Frontend

Each of the front service (admin, b2b, msf, etc.) should firstly request a websocket authorization token through.

GET https://channels.ampli-solutions.com/authorize

```cs 
Authorization Header: Bearer TOKEN
```
replace TOKEN with WS token

Code implementation - package [socket.io](www.socket.io)
```cs
<script src="/socket.io/socket.io.js"></script>
```
For all the other methods of including socket.io package to your project, please refer to documentation:
[client-api](https://socket.io/docs/v4/client-api/)


After receiving a token, use it to get your websocket connection

```cs
const url = 'https://channels.ampli-solutions.com/'
const token = token received from "authorize" API
const keycloakID = 'your keycloak'
const channel = 'msf' (channel used by your project) (b2b, admin, msf, etc)
const socket = io(url, {
	auth: {
		token:token,
		keycloakID:keycloakID,
		channel:channel
	}
})
```
Add a **notification** type event listener to your socket:
```cs
socket.on('notification', function(msg) {
	//here goes your action code that runs after notification is received
	(msg) - notification message object (string lub json)
})
```


## Backend

You have 3 variants of notifications:
1. notifying every user of given channel (msg, b2b, admin, etc.),
2. notifiyng every channel that a given keycloak user is using,
3. notifying a single channel of given keycloak user

API Endpoint:
POST https://channels.ampli-solutions.com/notify

BODY:
```cs
{
	"API_KEY": "5b28dc2f-...-7fbb705907c5", //current api key//Required
	"msg" : string/json, //Required
	"keycloak": "26259ede-....-9c58927efaae",//keycloak of the target user for notification(variant 2 and 3)
	"channel": "msf"//(b2b, admin, etc)//when you want to use variant 1 or 2 notification
}
```
Summary:
Variant 1 requires only the **channel** variable in the body,
Variant 2: only **keycloak**
Variant 3: both **channel** and **keycloak**