-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from linomp/feat/local-dev-mqtt
Removed cloud dependency for local mqtt dev
- Loading branch information
Showing
5 changed files
with
94 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
version: "3.8" | ||
|
||
services: | ||
hivemq-broker: | ||
image: hivemq/hivemq-ce | ||
ports: | ||
- "8883:1883" | ||
- "8884:8000" | ||
networks: | ||
- my-network | ||
restart: always | ||
|
||
networks: | ||
my-network: | ||
|
||
# docker compose -f docker-compose-dev.yml up -d | ||
# docker compose -f docker-compose-dev.yml down -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,62 @@ | ||
import mqtt, { type Packet } from "mqtt"; | ||
import type { GameSessionDTO, MqttFrontendConnectionDetails } from "src/api/generated"; | ||
import mqtt from "mqtt"; | ||
import type {GameSessionDTO, MqttFrontendConnectionDetails} from "src/api/generated"; | ||
|
||
const buildUrlAndOpts = (connectionDetails: MqttFrontendConnectionDetails): [string, mqtt.IClientOptions] => { | ||
const brokerUrl = connectionDetails.host != "localhost" ? `wss://${connectionDetails.host}:${connectionDetails.port}/mqtt` : | ||
`ws://${connectionDetails.host}:${connectionDetails.port}/mqtt`; | ||
|
||
const opts: mqtt.IClientOptions = { | ||
username: connectionDetails.host != "localhost" ? connectionDetails.username : undefined, | ||
password: connectionDetails.host != "localhost" ? connectionDetails.password : undefined, | ||
protocolVersion: 4, // MQTT 3.1.1 | ||
}; | ||
|
||
return [brokerUrl, opts]; | ||
} | ||
|
||
export const getClient = async ( | ||
connectionDetails: MqttFrontendConnectionDetails, | ||
messageHandler: ( | ||
topic: string, | ||
message: Buffer, | ||
) => Promise<void> | ||
connectionDetails: MqttFrontendConnectionDetails, | ||
messageHandler: ( | ||
topic: string, | ||
message: Buffer, | ||
) => Promise<void> | ||
): Promise<[mqtt.MqttClient, () => void]> => { | ||
|
||
const brokerUrl = `wss://${connectionDetails.host}:${connectionDetails.port}/mqtt`; | ||
const [brokerUrl, opts] = buildUrlAndOpts(connectionDetails); | ||
|
||
if (import.meta.env.VITE_DEBUG) { | ||
console.log(`Connecting to MQTT broker at ${brokerUrl}`); | ||
console.log(`Base topic: `, connectionDetails.base_topic); | ||
} | ||
|
||
try { | ||
try { | ||
const client = await mqtt.connectAsync( | ||
brokerUrl, | ||
opts | ||
); | ||
|
||
await client.subscribeAsync(`${connectionDetails.base_topic}`); | ||
|
||
client.on("message", async (topic, message): Promise<void> => { | ||
if (import.meta.env.VITE_DEBUG) { | ||
const casted = JSON.parse(message.toString()) as GameSessionDTO; | ||
console.log(`Received message on topic ${topic}`, casted); | ||
} | ||
await messageHandler(topic, message); | ||
}); | ||
|
||
return [ | ||
client, | ||
() => { | ||
if (import.meta.env.VITE_DEBUG) { | ||
console.log(`Connecting to MQTT broker at ${brokerUrl}`); | ||
console.log(`Base topic: `, connectionDetails.base_topic); | ||
console.log(`Unsubscribing from topic ${connectionDetails.base_topic}`); | ||
} | ||
|
||
const client = await mqtt.connectAsync( | ||
brokerUrl, | ||
{ | ||
username: connectionDetails.username, | ||
password: connectionDetails.password, | ||
protocolVersion: 4, // MQTT 3.1.1 | ||
} | ||
); | ||
|
||
await client.subscribeAsync(`${connectionDetails.base_topic}`); | ||
|
||
client.on("message", async (topic, message): Promise<void> => { | ||
if (import.meta.env.VITE_DEBUG) { | ||
const casted = JSON.parse(message.toString()) as GameSessionDTO; | ||
console.log(`Received message on topic ${topic}`, casted); | ||
} | ||
await messageHandler(topic, message); | ||
}); | ||
|
||
return [ | ||
client, | ||
() => { | ||
if (import.meta.env.VITE_DEBUG) { | ||
console.log(`Unsubscribing from topic ${connectionDetails.base_topic}`); | ||
} | ||
client.unsubscribe(`${connectionDetails.base_topic}`); | ||
} | ||
]; | ||
|
||
} catch (error) { | ||
console.error(`Error connecting to MQTT broker ${brokerUrl}: `, error); | ||
throw error; | ||
} | ||
} | ||
client.unsubscribe(`${connectionDetails.base_topic}`); | ||
} | ||
]; | ||
|
||
} catch (error) { | ||
console.error(`Error connecting to MQTT broker ${brokerUrl}: `, error); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters