NodeJS Quickstart
You can use any language or framework in a session backend. If you can run it in a Docker container, you can run it on Jamsocket. This quickstart will cover the essential setup for running a NodeJS backend.
Note: we’ll be using Docker’s command-line tool and NodeJS (>=18), so get those installed if you haven’t already.
Write your session backend code and Dockerfile
Let’s create the following files for our session backend in a directory called app
.
const http = require('http')
const PORT = process.env.PORT || 8080 // Your session backend must run on port 8080 for Jamsocket to find it.
const server = http.createServer((req, res) => {
console.log('Received a request!')
res.end('Hello World')
})
server.listen(PORT, () => {
console.log(`Server running at http://0.0.0.0:${PORT}/`)
})
To package your session backend into a container, write a Dockerfile that Jamsocket can use to build and push your session backend code.
FROM node:18-slim
# Set the working directory of the container
WORKDIR /app
COPY server.js .
CMD ["node", "server.js"]
Developing your code locally
Run the Dev CLI
npx jamsocket dev --dockerfile ./Dockerfile --watch .
npx jamsocket dev
will start a Jamsocket dev server at localhost:8080
. Check out the Dev CLI for more details on how the Dev CLI works.
Connect to a session backend
Get a Connection URL
To connect to your session backend, use the Jamsocket HTTP API or client libraries to get a connection URL.
Try making a curl request to the connect endpoint from the HTTP API:
curl -X POST http://localhost:8080/v2/service/MY_ACCOUNT/MY_SERVICE/connect
A successful JSON response will include a connection url. Use this URL to access your session backend API.
{
"backend_id": "ebb8ljmndix2ge",
"spawned": true,
"status": "scheduled"
"token": "ClpYXET8OA-ie7ufWFHE5y0Q-NbghE04niQ2fwBZe40",
"url": "http://localhost:9090/ClpYXET8OA-ie7ufWFHE5y0Q-NbghE04niQ2fwBZe40/",
"secret_token": "129aky7mT0ym-x0w5I88eTn41FFJawYwV4EgabC4r12",
"status_url": "http://localhost:8080/v2/backend/ebb8ljmndix2ge/status",
"ready_url": ""
}
In development, you don’t need to supply the endpoints with real account or service information. The next step, deploying to production, will cover account and service creation.
Connect to your session backend API
When the backend returned from the connect
endpoint shows a Ready
status, make a curl request to an endpoint in your session backend api using the connection url.
A GET request to /
should return the Hello World
string from the backend.
curl CONNECTION_URL_FROM_SPAWN_REQUEST
Deploy to Jamsocket
Login to Jamsocket
npx jamsocket login
Create a service
npx jamsocket service create my-hello-world
You should see the service appear on your Jamsocket dashboard.
Build and push your session backend code to Jamsocket
You can build your session backend docker image and push to Jamsocket’s container registry with a single command:
npx jamsocket push my-hello-world --dockerfile ./Dockerfile
Connect to session backends in production
To use the HTTP API in production, create an API token from the Jamsocket dashboard. You’ll also need to provide your account and service name in the connect endpoint.
A connect request should look like this:
curl \
-X POST \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d "{}" \
https://api.jamsocket.com/v2/service/ACCOUNT_NAME/my-hello-world/connect
Just like in development, making a request to the connection url should return a the hello world string from the backend.
curl CONNECTION_URL_FROM_SPAWN_REQUEST
See your session backend’s logs
Finally, you can view logs from your session backends in Production with the Jamsocket CLI.
npx jamsocket logs BACKEND_NAME
Or view logs from your dashboard when you click on the backend name in the Service page.