PubNub Portal

Two More Reasons why PubNub Functions are Awesome

Darryn Campbell on Jan 13, 2025
Two More Reasons why PubNub Functions are Awesome

This is part 2 of a 2-part series on why PubNub Functions are Awesome. The first part is here if you missed it.

PubNub Functions are serverless code triggered as events pass through on the PubNub platform. Functions let you write code or leverage existing integrations to transform, re-route, augment, filter, and aggregate data.

PubNub Functions do not require you to spin up a dedicated backend. They are distributed, meaning as your application grows, they will automatically scale so your application can enjoy low latency and 99.999% uptime. Functions support several built-in libraries as well as custom integrations that offer preconfigured connections to third-party services and partners.

How do I Translate Messages in Real-Time?

Real-time language translation within your chat will benefit everybody.

  • For apps with a social aspect, like games, users will benefit from wider and more diverse communities. While your application's adoption is growing, users will quickly feel part of a larger (global) community, accelerating your app’s adoption.

  • Businesses can benefit from multilingual customer support, allowing advisors to assist customers from different regions in their native languages.

  • E-commerce applications can immediately widen their customer base by connecting buyers and sellers in different countries.

PubNub Functions can intercept messages and pass them to an external service through a REST API, which makes it easy to switch translation providers if you need to at any point in the future. We have several custom integrations that allow you to translate messages in real-time, but you can easily call any external REST API from a PubNub Function through the XHR module, meaning Functions can support your chosen provider, whichever that might be. I will give code examples for both approaches below.

From personal experience, I would recommend against using AI for translation, as I have had better experience with dedicated services, but your mileage may vary.

Your workflow will be as follows: your client application sends a message to PubNub in the user’s native language, which a PubNub Function intercepts. The message is then sent to your chosen translation service, and the translated text is returned. Note that many translation services support the translation to multiple languages in a single API call, which will help if you have speakers of many different languages in your group chat. After translation, the message is sent to each receiver. Note that there will be a delay while the message is translated, and you will need some way of allowing each user to specify which language they want to receive messages in.

Other considerations include message moderation and sentiment analysis - ensure your wider chat solution is able to support all languages your users might speak.

Set up Functions for Translating Messages in Real-Time

To create a PubNub Function, first log into the PubNub Portal at admin.pubnub.com (if you don’t have an account, you can sign up for free). Create an application to house your Function by selecting ‘Apps’ from the left-hand menu, then click ‘+ Create app.’ At the time of writing, we are in the process of transitioning to a new version of Functions - please follow the documentation that matches your situation, either Functions v1 or Functions.

Be sure to specify the ‘Before Publish or Fire’ event type since moderation requires changing messages in flight.

Alternatively, many of our custom integrations have a ‘Try it’ button. This button will guide you through creating the correct type of Function on your keyset and prepopulate it with a code template you can modify as needed.

Function Source Code for Translating Chat Messages

PubNub Function to Translate Messages using AWS translate

The following code is taken from our guide to Add Moderation and Translation to your Unity Game and is an example of using our AWS translate integration.

PubNub Function to Translate Messages using DeepL

DeepL is a very popular translation solution, to call this service from a PubNub function, you can invoke it directly using a REST API call:

Next Steps: Translating Messages in Real-Time

You might also find this blog interesting:

We also have the following related custom integrations: Amazon Translate, IBM Watson, Microsoft Translator, Hugging Face - Some ML models support translation

Sign up for a free PubNub account to start exploring Functions today, or check out our Functions documentation.

How do I Add Location Context to my Data?

PubNub handles 3 trillion real-time API calls a month (with a ‘T’), which is a lot of messages going across our network. Although the payload of those messages can be any serializable data, most customers only exchange a single text string, but you can do much more. By adding context to messages, you can create a more immersive experience, for example, a message might be marked as ‘high priority’, or a chat application might encode quoted or forwarded messages.

Adding location context to messages can be useful in several ways:

  • Time zones can help ensure messages are delivered to the recipient at a sensible time, or the sender can be warned if the recipient is in a different timezone.

  • Geographical latitude and longitude can be used to calculate directions or recommend nearby activities.

  • A thumbnail image of a map can be automatically generated alongside an address, enhancing the user experience.

It is easy to compartmentalize location messages and assume they only apply to specific industries, such as delivery or tracking use cases, but this is not the case. Location context can enhance almost all messages and industries, from social chat to IoT device monitoring.

The workflow is summarized in the diagram below. Your client application sends a message that a PubNub Function intercepts. A third-party service can then be used to provide the location context, which is added to the message before it is forwarded to its intended recipients. Do not fall into the common pitfall of assuming that Google Maps is always the best solution for your intended use case. While Google offers a large variety of map APIs, other companies, such as TomTom or Mapbox, offer developer APIs focused exclusively on mapping.

As shown on the diagram, the ‘client’ application can be any platform supported by PubNub; while this is commonly a mobile device, it can also be a dashboard to, for example, provide an overview of all users or deliveries en-route.

Set up Functions for Adding Location Context to Real-Time Data

To create a PubNub Function, first log into the PubNub Portal at admin.pubnub.com (if you don’t have an account, you can sign up for free). Create an application to house your Function by selecting ‘Apps’ from the left-hand menu, then click ‘+ Create app.’ At the time of writing, we are in the process of transitioning to a new version of Functions - please follow the documentation that matches your situation, either Functions v1 or Functions.

Alternatively, many of our custom integrations have a ‘Try it’ button. This button will guide you through creating the correct type of Function on your keyset and prepopulate it with a code template you can modify as needed.

Function Source Code for Adding Location Context

PubNub Function that calls the Google Maps Geocode API

The following code snippet shows how to call Google’s Geocode API to convert an address to a latitude and longitude. This uses the Functions XHR Module to invoke the Geocode REST API directly:

PubNub Function that calls the Mapbox MapMatching API

The following is an abridged portion of code from our article on Smoothing Location Data & Snapping to the Nearest Road, which shows how to use the Mapbox MapMatching API from a PubNub Function:

Next Steps:

You might also find these blogs interesting:

We also have the following related custom integrations: Mapbox Static Map, Mapbox Geolocation, Mapbox Geocoding, Mapbox Directions, ESRI geocoding, ESRI directions.

Related: Although they do not use Functions, you might also find the following demos interesting as they use Google Maps for directions and route finding: Delivery Demo, Transport & Logistics Demo.

Sign up for a free PubNub account to start exploring Functions today, or check out our Functions documentation.

And Finally: Recent Updates to PubNub Functions

PubNub has recently implemented a slew of enhancements to our Functions to make them easier to use, with additional capabilities:

  • A Functions API makes it easier to deploy & manage new and existing Functions

  • We have enhanced our in-browser editor to allow on-the-fly editing

  • We now retain a history of previous deployments, so you can easily roll back changes if something is not working.

  • We have added new built-in libraries to support UUID, JWT, and JSONPath Plus.

Sign up for a free PubNub account to start exploring Functions today, or check out our Functions documentation for more information.