For node.js
Setup
The node.js setup follows a very similar approach to setting up a js project for web. The differences between the node.js setup and browser setup are:
The node.js SDK is meant to be used with an application principal i.e. it is supposed to be used with an access key/access secret pair. It does not support user-based authorization for performing API calls.
The node.js SDK does not support any messaging pipelines and is only designed to provide a service layer to mitter.io since it is designed to be used for projects implementing an application backend.
Do note that with a websocket polyfill, you can still use
@mitter-io/web
in your node.js project, but this is currently not supported. In future versions we will be documenting the usage of the@mitter-io/web
package within a node.js application.
To install the node.js SDK, add the following dependencies in your application
or with npm
To initialize a mitter object, use the following:
Do note that the access key object is not the direct JSON you get when you press on Copy to clipboard
on the mitter.io panel. That follows a structure with an additional level of structuring and is used for file-based credential loading by other SDKs (for example, Java)
For the <mitter-api-url>
, use the following value:
If you're using the cloud hosted solution, you can omit the
mitterApiBaseUrl
key in the config or explicitly set it tohttps://api.mitter.io
If you're running it as a docker container set it to
http://localhost:<port>
where the port is port forwarded by docker for11902
. To find out which port it is, run the following commanddocker port $(docker ps --filter expose=11901-11903/tcp --format "")
Once you have the mitter.io object, most of the operations are similar to using the web package. For instance, a common use-case is to get tokens for a user in your application.
Implementing a Token Server
An example project for this is located on our public gitlab repository
In our getting started sections we hard-coded our user authentication tokens directly in our code. It goes without saying that it is not a great practice for production applications. To implement a token service, what we first need is a repository of our users and optionally some mapping of their credentials. For this example, we'll use a simple JS dictionary:
What we also need the backend to do is create the user in mitter.io if it doesn't exist. To make this resilient, we'll model this around get-or-create semantics. What our service will have is:
Have a
/login
endpoint that accepts a request with parameters{username: '', password: ''}
and authenticates it against the store.If a user is authenticated, it creates a user in mitter.io. If the request to create a user fails due to the user already existing, we will ignore the error. Any other error will be reported in our request as a
500.
We will then fetch a user token for the user and return this to the front-end along with the mitter.io user id we got from the previous step.
An explanation of the code above:
We first iterate over our user mapping to check if a user with the given credentials exists. In a production application, this would connect to your database, ldap or any other user storage you might be using.
If the user is found, then we create a user with the same id in mitter.io. We don't have to specify a user id, or use the same user id at all, but usually having the same user id makes things simple. It is recommended that you map your domain ids directly as mitter.io ids or map them with some prefix. If a user is not found, we simply return a
401
saying that the user was not authenticated.The create user request might fail in case the user was already created. Whenever any operation fails due to a unique-constraint violation, mitter.io will return a
409 CONFLICT
HTTP status and the response body will have a fielderrorCode
set toduplicate_entity
. Refer to the platform reference for a complete list of error scenarios and their respective error codes and HTTP status codes.If we haven't encountered an error so far, it means that a user with the same id as was mapped to the username provided to our token server as a request exists in mitter.io and the provided credentials have matched. We then use the
userAuthClient
to fetch a user access token for the user and then send a successful login response to the frontend. TheloginSuccessfulResponse
could be any function that combines the the user information, the mitter.io user information and any additional data and passes on this data to the fronted. An example would look like:
Last updated