Socket
Class that defines the different Actions to Api Gateway WebSocket.
Basic use:
import { Api, ApiSocket } from "micro-lambda-api";
const api = new Api();
const socket = new ApiSocket();
socket.action("message", (req, res) => {
// Process and return data
});
app.use(socket.actions()).use(socket.middlewares());
Example: Sending a message from the Server to the Application
import { ApiGatewayManagementApi } from "aws-sdk";
socket
.action("message", async (req, res) => {
const connectionId = req.connectionId || "";
const apiWs = new ApiGatewayManagementApi({
apiVersion: "2018-11-29",
endpoint: `https://${req.host}/${req.stage}`,
});
await apiWs
.postToConnection({
ConnectionId: connectionId,
Data: // My Custom Data in JS Object,
})
.promise();
res.send("Message sent!!!");
});
Last updated
Was this helpful?